Skip to content

Commit 17a7fea

Browse files
committed
Add post api test
1 parent 8a919d5 commit 17a7fea

File tree

3 files changed

+98
-30
lines changed

3 files changed

+98
-30
lines changed

test/main.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Application, Router } from 'https://deno.land/x/oak/mod.ts'
1+
import { Application, Router, helpers, RouterContext, RouteParams } from 'https://deno.land/x/oak/mod.ts'
22

33
const env = Deno.env.toObject()
44
const HOST = env.HOST || '0.0.0.0'
@@ -26,9 +26,10 @@ let books: Array<IBook> = [{
2626

2727
const searchBookById = (id: string): (IBook | undefined) => books.filter(book => book.id === id)[0]
2828

29-
const getBook = ({ params, response }: { params: { id: string }; response: any }) => {
30-
console.log(params)
31-
const book: IBook | undefined = searchBookById(params.id)
29+
const getBook = (ctx: RouterContext<any>) => {
30+
const { response }: { response: any } = ctx;
31+
const query = helpers.getQuery(ctx);
32+
const book: IBook | undefined = searchBookById(query.id)
3233
if (book) {
3334
response.status = 200
3435
response.body = book
@@ -38,10 +39,34 @@ const getBook = ({ params, response }: { params: { id: string }; response: any }
3839
}
3940
}
4041

42+
const postBook = async (ctx: RouterContext<RouteParams>) => {
43+
const { request, response } = ctx;
44+
if (request.hasBody) {
45+
const result: any = await request.body({
46+
contentTypes: {
47+
text: ["application/javascript"],
48+
},
49+
});
50+
const body: any = JSON.parse(result.value)
51+
const book: IBook | undefined = searchBookById(body.id)
52+
console.log(body)
53+
if (!book) {
54+
response.status = 200
55+
response.body = body
56+
} else {
57+
response.status = 422
58+
response.body = { message: `Book already exists.` }
59+
}
60+
} else {
61+
response.status = 422
62+
response.body = { message: `Can't parse body.` }
63+
}
64+
}
65+
4166
const router = new Router()
42-
router.get('/books/:id', getBook)
67+
router.get('/books', getBook)
68+
.post('/books', postBook)
4369
// .get('/books', getBooks)
44-
// .post('/books', addBook)
4570
// .put('/books/:id', updateBook)
4671
// .delete('/books/:id', deleteBook)
4772

test/test.json

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
{
2-
"testCases":{
3-
"get_api":{
4-
"path": "/books/1"
2+
"testCases": {
3+
"get_api": {
4+
"path": "/books",
5+
"query": {
6+
"id": "1"
7+
}
58
},
6-
"post_api":{
7-
"path": "/books/1",
8-
"method": "POST"
9+
"post_api": {
10+
"path": "/books",
11+
"method": "POST",
12+
"body": {
13+
"id": "5",
14+
"author": "Robin Wieruch",
15+
"title": "The Road to React"
16+
}
17+
},
18+
"invalid_post_api": {
19+
"path": "/books",
20+
"method": "POST",
21+
"body": {
22+
"id": "1",
23+
"author": "Robin Wieruch",
24+
"title": "The Road to React"
25+
}
926
}
1027
},
11-
"url": "localhost:3000"
28+
"url": "localhost:3000",
29+
"header": {
30+
"Content-Type": "application/javascript"
31+
}
1232
}

test/test.ts

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,56 @@ async function run(testCase: string) {
1414
});
1515
const output = await cmd.output();
1616
cmd.close();
17-
const outStr = new TextDecoder().decode(output).replace(/META:\n(.*\n){4}/, "").split("\n").filter((x) =>
18-
x !== ""
19-
).map((x) =>
20-
x.replace(
21-
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
22-
"",
23-
)
17+
const outStr = new TextDecoder().decode(output).replace(
18+
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
19+
"",
2420
);
2521
return outStr;
2622
}
2723

2824
Deno.test({
29-
name: "Get APIs ",
25+
name: "GET API",
3026
async fn() {
31-
let expected = `
32-
Running Case: get_api
33-
Response:
34-
200 OK
35-
BODY:
27+
const response = await run("get_api");
28+
expect(response.includes("Running Case: get_api")).toBeTruthy();
29+
expect(response.includes("200 OK")).toBeTruthy();
30+
expect(response.includes(
31+
`BODY:
3632
{
3733
"id": "1",
3834
"author": "Robin Wieruch",
3935
"title": "The Road to React"
40-
}
41-
`.split("\n").filter((x) => x !== "");
42-
const response = await run("get_api");
43-
expect(response.map((x, i) => expected[i] === x).filter(x => !x).length).toEqual(0);
36+
}`,
37+
)).toBeTruthy();
38+
expect(response.includes(
39+
`META:
40+
{
41+
"ResponseTime":`,
42+
)).toBeTruthy();
43+
},
44+
sanitizeResources: false,
45+
sanitizeOps: false,
46+
});
47+
48+
Deno.test({
49+
name: "POST API",
50+
async fn() {
51+
const response = await run("post_api");
52+
expect(response.includes("Running Case: post_api")).toBeTruthy();
53+
expect(response.includes("200 OK")).toBeTruthy();
54+
expect(response.includes(
55+
`BODY:
56+
{
57+
"id": "5",
58+
"author": "Robin Wieruch",
59+
"title": "The Road to React"
60+
}`,
61+
)).toBeTruthy();
62+
expect(response.includes(
63+
`META:
64+
{
65+
"ResponseTime":`,
66+
)).toBeTruthy();
4467
},
4568
sanitizeResources: false,
4669
sanitizeOps: false,

0 commit comments

Comments
 (0)