Skip to content

Commit a83d1f4

Browse files
committed
feat: post api template and tests
1 parent 09ff3d7 commit a83d1f4

File tree

5 files changed

+129
-4
lines changed

5 files changed

+129
-4
lines changed

src/api/hello.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@ const schema = {
2424
}
2525
};
2626

27+
const postSchema = {
28+
schema: {
29+
body: {
30+
type: 'object',
31+
required: ['name'],
32+
properties: {
33+
name: {
34+
type: 'string',
35+
minLength: 1,
36+
maxLength: 10
37+
}
38+
}
39+
},
40+
response: {
41+
200: { //HTTP_STATUS_CODES.OK
42+
type: 'object',
43+
required: ['message'],
44+
properties: {
45+
message: {type: 'string'}
46+
}
47+
}
48+
}
49+
}
50+
};
51+
2752
export function getHelloSchema() {
2853
return schema;
2954
}
@@ -35,3 +60,14 @@ export async function hello(request, _reply) {
3560
};
3661
return response;
3762
}
63+
64+
export function getHelloPostSchema() {
65+
return postSchema;
66+
}
67+
export async function helloPost(request, _reply) {
68+
const name = request.body.name;
69+
const response = {
70+
message: `hello ${name}`
71+
};
72+
return response;
73+
}

src/server.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import fastify from "fastify";
2020
import {init, isAuthenticated, addUnAuthenticatedAPI} from "./auth/auth.js";
2121
import {HTTP_STATUS_CODES} from "@aicore/libcommonutils";
2222
import {getConfigs} from "./utils/configs.js";
23-
import {getHelloSchema, hello} from "./api/hello.js";
23+
import {getHelloSchema, hello, getHelloPostSchema, helloPost} from "./api/hello.js";
2424
import {fastifyStatic} from "@fastify/static";
2525

2626
import path from 'path';
@@ -77,6 +77,11 @@ server.get('/hello', getHelloSchema(), function (request, reply) {
7777
return hello(request, reply);
7878
});
7979

80+
addUnAuthenticatedAPI('/helloPost');
81+
server.post('/helloPost', getHelloPostSchema(), function (request, reply) {
82+
return helloPost(request, reply);
83+
});
84+
8085
// An authenticated version of the hello api
8186
server.get('/helloAuth', getHelloSchema(), function (request, reply) {
8287
return hello(request, reply);

test/integration/hello.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ describe('Integration Tests for hello api', function () {
2121
expect(output).eql({message: "hello world"});
2222
});
2323

24+
it('should say hello Post without auth', async function () {
25+
let output = await fetch("http://localhost:5000/helloPost", {
26+
method: 'post',
27+
body: JSON.stringify({name: "ola"}),
28+
headers: {'Content-Type': 'application/json'}
29+
});
30+
output = await output.json();
31+
expect(output).eql({message: "hello ola"});
32+
});
33+
34+
it('should hello Post say 400 without required args', async function () {
35+
let output = await fetch("http://localhost:5000/helloPost", {
36+
method: 'post',
37+
body: JSON.stringify({}),
38+
headers: {'Content-Type': 'application/json'}
39+
});
40+
output = await output.json();
41+
expect(output.statusCode).eql(400);
42+
});
43+
2444
it('should say helloAuth if authorised', async function () {
2545
let output = await fetch("http://localhost:5000/helloAuth?name=world", { method: 'GET', headers: {
2646
authorization: "Basic hehe"

test/unit/api/hello.spec.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*global describe, it*/
22
import * as chai from 'chai';
3-
import {hello, getHelloSchema} from "../../../src/api/hello.js";
4-
import {getSimpleGetReply, getSimpleGETRequest} from '../data/simple-request.js';
3+
import {hello, getHelloSchema, helloPost, getHelloPostSchema} from "../../../src/api/hello.js";
4+
import {getSimpleGetReply, getSimpleGETRequest, getSimplePOSTReply, getSimplePOSTRequest} from '../data/simple-request.js';
55
import Ajv from "ajv";
66

77
export const AJV = new Ajv();
@@ -16,7 +16,7 @@ describe('unit Tests for hello api', function () {
1616
expect(helloResponse).eql({message: 'hello rambo'});
1717
});
1818

19-
it('should validate schemas for sample request/responses', async function () {
19+
it('should validate schemas for sample GET request/responses', async function () {
2020
let request = getSimpleGETRequest();
2121
// request
2222
const requestValidator = AJV.compile(getHelloSchema().schema.querystring);
@@ -29,4 +29,18 @@ describe('unit Tests for hello api', function () {
2929
let response = await hello(getSimpleGETRequest(), getSimpleGetReply());
3030
expect(successResponseValidator(response)).to.be.true;
3131
});
32+
33+
it('should validate schemas for sample POST request/responses', async function () {
34+
let request = getSimplePOSTRequest();
35+
// request
36+
const requestValidator = AJV.compile(getHelloPostSchema().schema.body);
37+
expect(requestValidator(request.body)).to.be.true;
38+
// message too long validation
39+
request.body.name = "a name that is too long";
40+
expect(requestValidator(request.body)).to.be.false;
41+
// response
42+
const successResponseValidator = AJV.compile(getHelloPostSchema().schema.response["200"]);
43+
let response = await helloPost(getSimplePOSTRequest(), getSimplePOSTReply());
44+
expect(successResponseValidator(response)).to.be.true;
45+
});
3246
});

test/unit/data/simple-request.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,54 @@ export function getSimpleGetReply() {
7272
return {...simpleGetReply};
7373
}
7474

75+
const simplePostRequest = {
76+
id: "req-2",
77+
params:{},
78+
query:{},
79+
body: {
80+
"name": "rambo"
81+
},
82+
raw:{
83+
httpVersionMajor: 1,
84+
httpVersionMinor: 1,
85+
httpVersion: "1.1",
86+
complete: false,
87+
rawHeaders: [
88+
"content-length",
89+
"21",
90+
"accept-encoding",
91+
"gzip, deflate, br",
92+
"Accept",
93+
"*/*",
94+
"User-Agent",
95+
"Thunder Client (https://www.thunderclient.com)",
96+
"Content-Type",
97+
"application/json",
98+
"Host",
99+
"localhost:5000",
100+
"Connection",
101+
"close"
102+
],
103+
rawTrailers: [],
104+
aborted: false,
105+
upgrade: false,
106+
url: "/helloPost",
107+
method: "POST",
108+
statusCode: null,
109+
statusMessage: null
110+
},
111+
log: logger
112+
};
113+
export function getSimplePOSTRequest() {
114+
return {...simplePostRequest};
115+
}
116+
117+
const simplePOSTReply = {
118+
request: simpleGETRequest,
119+
log: logger,
120+
raw: {}
121+
};
122+
export function getSimplePOSTReply() {
123+
return {...simplePOSTReply};
124+
}
75125

0 commit comments

Comments
 (0)