Skip to content

Commit

Permalink
Merge pull request #14 from XenitAB/basic-request-checking
Browse files Browse the repository at this point in the history
Basic request checking
  • Loading branch information
dachrillz authored Dec 14, 2022
2 parents 38ec8d7 + f4fff90 commit 05dd717
Show file tree
Hide file tree
Showing 13 changed files with 402 additions and 38 deletions.
23 changes: 23 additions & 0 deletions scenarios/simple/checkRequestBody/http.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"log": {
"entries": [
{
"request": {
"method": "POST",
"url": "http://localhost/ping",
"postData": {
"mimeType": "application/json",
"text": "{\"hello\":\"world\"}"
}
},
"response": {
"status": 200,
"content": {
"mimeType": "text/plain",
"text": "{\"hello\":\"world\"}"
}
}
}
]
}
}
23 changes: 23 additions & 0 deletions scenarios/simple/checkRequestBody/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
openapi: 3.0.0
info:
title: Minimal example
version: 0.1.0
servers:
- url: "http://localhost:8000"
paths:
/ping:
post:
requestBody:
description: desc
content:
application/json:
schema:
type: string
responses:
'200':
description: Returns `pong`
content:
text/plain:
schema:
type: string
example: pong
23 changes: 23 additions & 0 deletions scenarios/simple/requestBodyNotDefined/http.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"log": {
"entries": [
{
"request": {
"method": "GET",
"url": "http://localhost/ping",
"postData": {
"mimeType": "application/json",
"text": "{\"hello\":\"world\"}"
}
},
"response": {
"status": 200,
"content": {
"mimeType": "text/plain",
"text": "{\"hello\":\"world\"}"
}
}
}
]
}
}
17 changes: 17 additions & 0 deletions scenarios/simple/requestBodyNotDefined/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
openapi: 3.0.0
info:
title: Minimal example
version: 0.1.0
servers:
- url: "http://localhost:8000"
paths:
/ping:
get:
responses:
'200':
description: Returns `pong`
content:
text/plain:
schema:
type: string
example: pong
19 changes: 19 additions & 0 deletions scenarios/simple/specRequiredRequestBodyButNoneInRequest/http.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"log": {
"entries": [
{
"request": {
"method": "POST",
"url": "http://localhost/ping"
},
"response": {
"status": 200,
"content": {
"mimeType": "text/plain",
"text": "{\"hello\":\"world\"}"
}
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.0.0
info:
title: Minimal example
version: 0.1.0
servers:
- url: "http://localhost:8000"
paths:
/ping:
post:
requestBody:
required: true
description: desc
content:
application/json:
schema:
type: string
responses:
'200':
description: Returns `pong`
content:
text/plain:
schema:
type: string
example: pong
18 changes: 5 additions & 13 deletions src/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,11 @@ const expected: Root = {
x_x_x_x_results: {
hits: 0,
},
"application/json": {
schema: expect.objectContaining({
type: "array",
items: expect.objectContaining({
properties: expect.objectContaining({
name: {
type: "string",
example: "doggie",
},
}),
}),
}),
},
"application/json": expect.objectContaining({
x_x_x_x_results: {
hits: 0,
},
}),
},
},
"400": {
Expand Down
33 changes: 26 additions & 7 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ const readApi = async (path: string): Promise<a.Root> => {
return api;
};

const getMimeType = (content: a.Content): a.MimeType | undefined => {
if (content["application/json"]) {
return a.newMimeType({
items: Object.keys(content["application/json"].schema).length,
});
}
};

const getContent = (response: a.ApiResponse): a.Content | undefined => {
// We only cover application/json for now
if (!response.content) {
Expand All @@ -15,10 +23,7 @@ const getContent = (response: a.ApiResponse): a.Content | undefined => {
x_x_x_x_results: a.newReportItem(),
};

if (response.content["application/json"]) {
contentToBeReturned["application/json"] =
response.content["application/json"];
}
contentToBeReturned["application/json"] = getMimeType(response.content);

return contentToBeReturned;
};
Expand Down Expand Up @@ -53,6 +58,20 @@ const getParameters = (parameters?: a.Parameter[]) => {
});
};

const getRequestBody = (
requestBody?: a.RequestBody
): a.RequestBody | undefined => {
if (!requestBody) {
return;
}

return {
content: getContent(requestBody),
required: requestBody.required,
x_x_x_x_results: a.newReportItem(),
};
};

const getOperation = (
name: a.HTTPMETHOD,
operation?: a.Operation
Expand All @@ -68,13 +87,13 @@ const getOperation = (
};

operationToBeAdded.parameters = getParameters(operation.parameters);
operationToBeAdded.requestBody; // TODO get request body
operationToBeAdded.requestBody = getRequestBody(operation.requestBody);
operationToBeAdded.responses = getResponses(operation.responses);

return operationToBeAdded;
};

const getPathNode = (pathName: string, path: a.Path): a.Path => {
const getPath = (pathName: string, path: a.Path): a.Path => {
const pathToBeAdded: a.Path = {
x_name: pathName,
x_x_x_x_results: a.newReportItem(),
Expand All @@ -94,7 +113,7 @@ const parseApi = (api: a.Root) => {

// Add paths
for (const [pathName, path] of Object.entries(api.paths)) {
const p = getPathNode(pathName, path);
const p = getPath(pathName, path);
root.paths[pathName] = p;
}

Expand Down
58 changes: 55 additions & 3 deletions src/api/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ export type HTTPMETHOD =
export type ApiError =
| "METHOD NOT FOUND"
| "PATH NOT FOUND"
| "STATUS NOT FOUND";
| "STATUS NOT FOUND"
| "REQUEST BODY NOT FOUND"
| "CONTENT WAS NOT FOUND"
| "BAD MIMETYPE"
| "REQUEST BODY WAS REQUIRED BUT NOTHING IN REQUEST";

export type ReportResult = {
hits: number;
Expand Down Expand Up @@ -122,15 +126,44 @@ export type Parameter = {
x_x_x_x_results: ReportResult;
};

export const newRequestBodyWithError = (error: ApiError): RequestBody => {
return {
required: false,
x_x_x_x_results: newReportItemWithError(error),
};
};

export const newRequestBody = (): RequestBody => {
return {
required: false,
x_x_x_x_results: newReportItem(),
};
};

export type RequestBody = {
required: boolean;
content: Content;
content?: Content;

x_x_x_x_results: ReportResult;
};

export const newContentBody = (): Content => {
return {
x_x_x_x_results: newReportItem(),
};
};

export const newContentBodyWithErrors = (error: ApiError): Content => {
return {
x_x_x_x_results: newReportItemWithError(error),
};
};

export type Mime = "application/json" | "text/json";

export type Content = {
"application/json"?: any;
"application/json"?: MimeType;
"text/json"?: MimeType;

x_x_x_x_results: ReportResult;
};
Expand All @@ -153,3 +186,22 @@ export type ApiResponse = {

x_x_x_x_results: ReportResult;
};

export const newMimeTypeWithError = (error: ApiError): MimeType => {
return {
schema: null,
x_x_x_x_results: newReportItemWithError(error),
};
};

export const newMimeType = (schema: any): MimeType => {
return {
schema: schema,
x_x_x_x_results: newReportItem(),
};
};

export type MimeType = {
schema: any;
x_x_x_x_results: ReportResult;
};
16 changes: 8 additions & 8 deletions src/httpParsing/parseHar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ const parseContent = (input: {
if (!input) {
return undefined;
}
if (input.mimeType !== "application/json") {
console.warn("Only application/json is supported!");
return undefined;
if (input.mimeType === "application/json" || input.mimeType === "text/json") {
return {
mimeType: input.mimeType,
text: input.text,
parsed: null,
};
}
return {
mimeType: input.mimeType,
text: input.text,
parsed: null,
};
console.warn("Only application/json and text/json is supported!");
return undefined;
};

const parseOneHar = (entry: Record<string, any>): har.t => {
Expand Down
Loading

0 comments on commit 05dd717

Please sign in to comment.