Skip to content

Commit

Permalink
Merge pull request #9 from Cliff99999/master
Browse files Browse the repository at this point in the history
Add files via upload
  • Loading branch information
STAT1C-Sy authored Apr 7, 2022
2 parents a5004ab + 4019b58 commit b37b881
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 16 deletions.
32 changes: 16 additions & 16 deletions examples/simple_test.ts
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"] });
});
29 changes: 29 additions & 0 deletions examples/test_setStatus_chain.ts
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"] });
});
20 changes: 20 additions & 0 deletions examples/test_with_body.ts
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"] });
});
24 changes: 24 additions & 0 deletions examples/test_with_headers.ts
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"] });
});
26 changes: 26 additions & 0 deletions examples/test_with_next.ts
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);
});

0 comments on commit b37b881

Please sign in to comment.