generated from BrunnerLivio/deno-module-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Cliff99999/master
Add files via upload
- Loading branch information
Showing
5 changed files
with
115 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { OpineRequest, OpineResponse } from "../deps.ts"; | ||
import { assertSpyCall, Spy } from "../deps.ts"; | ||
import { mockRequest, mockResponse } from "../mod.ts"; | ||
|
||
function opineRequestHandler(req: OpineRequest, res: OpineResponse) { | ||
res.send("message"); | ||
} | ||
|
||
Deno.test("Simple test with opine-unittest-utils", () => { | ||
const request = mockRequest(); | ||
const response = mockResponse(); | ||
|
||
opineRequestHandler(request, response); | ||
|
||
assertSpyCall(response.send as Spy<any>, 0, { args: ["message"] }); | ||
}); | ||
import { OpineRequest, OpineResponse } from "../deps.ts"; | ||
import { assertSpyCall, Spy } from "../deps.ts"; | ||
import { mockRequest, mockResponse } from "../mod.ts"; | ||
|
||
function opineRequestHandler(req: OpineRequest, res: OpineResponse) { | ||
res.send("message"); | ||
} | ||
|
||
Deno.test("Simple test with opine-unittest-utils", () => { | ||
const request = mockRequest(); | ||
const response = mockResponse(); | ||
|
||
opineRequestHandler(request, response); | ||
|
||
assertSpyCall(response.send as Spy<any>, 0, { args: ["message"] }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { OpineRequest, OpineResponse } from "../deps.ts"; | ||
import { assertSpyCall, Spy } from "../deps.ts"; | ||
import { mockRequest, mockResponse } from "../mod.ts"; | ||
|
||
function opineRequestHandler(req: OpineRequest, res: OpineResponse) { | ||
res.setStatus(401).send("Not allowed") | ||
} | ||
|
||
Deno.test("Simple test with headers", () => { | ||
|
||
|
||
const request = mockRequest({ | ||
headers: new Headers({ | ||
'apiKey': 'myKey' | ||
}), | ||
}); | ||
|
||
//need to create a second mock to allow chaining | ||
const sendMock = mockResponse(); | ||
|
||
const response = mockResponse({ | ||
setStatus: () => sendMock | ||
}); | ||
|
||
opineRequestHandler(request, response); | ||
|
||
assertSpyCall(response.setStatus as Spy<any>, 0, { args: [401] }); | ||
assertSpyCall(sendMock.send as Spy<any>, 0, { args: ["Not allowed"] }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { OpineRequest, OpineResponse } from "../deps.ts"; | ||
import { assertSpyCall, Spy } from "../deps.ts"; | ||
import { mockRequest, mockResponse } from "../mod.ts"; | ||
|
||
function opineRequestHandler(req: OpineRequest, res: OpineResponse) { | ||
res.send(req.body.someArg); | ||
} | ||
|
||
Deno.test("Simple test with body", () => { | ||
const request = mockRequest({ | ||
body: { | ||
someArg: "SomeValue" | ||
} | ||
}); | ||
const response = mockResponse(); | ||
|
||
opineRequestHandler(request, response); | ||
|
||
assertSpyCall(response.send as Spy<any>, 0, { args: ["SomeValue"] }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { OpineRequest, OpineResponse } from "../deps.ts"; | ||
import { assertSpyCall, Spy } from "../deps.ts"; | ||
import { mockRequest, mockResponse } from "../mod.ts"; | ||
|
||
function opineRequestHandler(req: OpineRequest, res: OpineResponse) { | ||
if(req.headers.get("apiKey") == "myKey") { | ||
res.send("valid"); | ||
} else { | ||
res.send("invalid"); | ||
} | ||
} | ||
|
||
Deno.test("Simple test with headers", () => { | ||
const request = mockRequest({ | ||
headers: new Headers({ | ||
'apiKey': 'myKey' | ||
}) | ||
}); | ||
const response = mockResponse(); | ||
|
||
opineRequestHandler(request, response); | ||
|
||
assertSpyCall(response.send as Spy<any>, 0, { args: ["valid"] }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { NextFunction, OpineRequest, OpineResponse } from "../deps.ts"; | ||
import { assertSpyCall, Spy } from "../deps.ts"; | ||
import { mockRequest, mockResponse, mockNextFunction } from "../mod.ts"; | ||
|
||
function opineRequestHandler(req: OpineRequest, res: OpineResponse, next: NextFunction) { | ||
if (req.headers.get("apiKey") == "myKey") { | ||
next(); | ||
} else { | ||
res.send("invalid"); | ||
} | ||
} | ||
|
||
Deno.test("Simple test with nextfunction", () => { | ||
const request = mockRequest({ | ||
headers: new Headers({ | ||
"apiKey": "myKey", | ||
}), | ||
}); | ||
const response = mockResponse(); | ||
|
||
const next = mockNextFunction(); | ||
|
||
opineRequestHandler(request, response, next); | ||
|
||
assertSpyCall(next as Spy<any>, 0); | ||
}); |