Skip to content

Commit

Permalink
feat: use multiple method options
Browse files Browse the repository at this point in the history
  • Loading branch information
xv2 committed Jul 10, 2023
1 parent 326bf05 commit aea2a6d
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ npx har-to-mocks [path to .har] [path mock/api folder] --dry-run

File can contain hundreds of requests so it's important to be able filter data. For filtering you can use flags:
- (`--url`) for filtering by match in the url. Search is case sensitive
- (`-m`, `--method=GET`) for filter specific method. Default value is 'GET'
- (`-m`, `--method=GET --method=POST`) for filter specific method. Supported: 'GET', 'POST', 'PUT', 'DELETE' and 'PATCH' methods. Default value is 'GET'.
- (`-t`, `--type=xhr`) for filtering request type. Default value is 'xhr'

Video example: [YouTube har-to-mocks@1.1.1](https://youtu.be/Pc2J8aHRKNY).
Expand Down
6 changes: 3 additions & 3 deletions src/har-to-mocks/har-to-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class HarToMocksProcess {
* @param {object} filter flags
*/
extract(fileContent: Har, filter: Filter) {
const { method, resourceType, url } = filter;
const { methods, resourceType, url } = filter;
const { entries } = fileContent.log;
let filtred: Entry[] = entries;

Expand All @@ -25,8 +25,8 @@ export class HarToMocksProcess {
filtred = filtred.filter((e) => e._resourceType === resourceType);
}

if (method) {
filtred = filtred.filter((e) => e.request.method === method);
if (methods) {
filtred = filtred.filter((e) => methods.includes(e.request.method));
}

// Log table with content
Expand Down
2 changes: 1 addition & 1 deletion src/har-to-mocks/types/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export enum ResourceType {
// ws = 'websocket',
}
export interface Filter {
method?: Method;
methods?: Method[];
resourceType?: ResourceType;
url?: string;
}
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ class HarToMocks extends Command {
// flag to filter by url
url: flags.string({ char: 'u', description: 'filter by url' }),
// flag to filter method (-m, --method=GET)
method: flags.enum<Method>({
method: flags.string({
char: 'm',
options: Object.values(Method),
description: 'filter by method',
default: Method.GET,
description: 'filter by method. You can use multiple options, for example: --method=GET --method=POST',
default: [Method.GET],
multiple: true,
}),
// flag to filter resourceType (-t, --type=xhr)
type: flags.enum<ResourceType>({
Expand Down Expand Up @@ -53,7 +54,11 @@ class HarToMocks extends Command {

if (args.file && typeof args.file === 'string') {
const data = (await readJson(args.file)) as Har;
process.extract(data, { method: usedFlags.method, resourceType: usedFlags.type, url: usedFlags.url });
process.extract(data, {
methods: usedFlags.method as Method[],
resourceType: usedFlags.type,
url: usedFlags.url,
});
}

if (args.to && typeof args.to === 'string') {
Expand Down
35 changes: 35 additions & 0 deletions tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,38 @@ Folder tree which will be applied:
`);
});
});

describe('Check multiple `--method` options', () => {
test
.stdout()
.do(() => cmd.run(['./tests/mocks/sample.har', './mocks', '--method=GET', '--method=POST']))
.it('should render with GET and POST requests', (ctx) => {
expect(ctx.stdout).toBe(`
Filtered requests:
Name Method Path
─────────────────────── ────── ───────────────────────────
userRoles GET /api/service/userRoles
currentUserId GET /api/service/currentUserId
active GET /api/service/clients/active
send POST /api/service/send
Folder tree which will be applied:
└─ mocks
└─ api
└─ service
├─ userRoles
│ └─ GET.json
├─ currentUserId
│ └─ GET.json
├─ clients
│ └─ active
│ └─ GET.json
└─ send
└─ POST.json
`);
});

});
110 changes: 110 additions & 0 deletions tests/mocks/sample.har
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,116 @@
"_blocked_queueing": 1.20900000911206,
"_blocked_proxy": 0.32400000000000007
}
},
{
"_priority": "High",
"_resourceType": "xhr",
"cache": {},
"connection": "25822",
"request": {
"method": "POST",
"url": "https://sample.com/api/service/send",
"httpVersion": "http/2.0",
"headers": [
{
"name": ":method",
"value": "POST"
},
{
"name": ":path",
"value": "/api/service/send"
},
{
"name": ":scheme",
"value": "https"
},
{
"name": "accept",
"value": "*/*"
},
{
"name": "accept-encoding",
"value": "gzip, deflate, br"
},
{
"name": "cache-control",
"value": "no-cache"
},
{
"name": "content-length",
"value": "6"
},
{
"name": "content-type",
"value": "application/x-www-form-urlencoded; charset=UTF-8"
},
{
"name": "pragma",
"value": "no-cache"
}
],
"queryString": [],
"cookies": [],
"headersSize": -1,
"bodySize": 6,
"postData": {
"mimeType": "application/x-www-form-urlencoded; charset=UTF-8",
"text": "s=text",
"params": [
{
"name": "s",
"value": "text"
}
]
}
},
"response": {
"status": 200,
"statusText": "",
"httpVersion": "http/2.0",
"headers": [
{
"name": "content-encoding",
"value": "gzip"
},
{
"name": "content-type",
"value": "text/html; charset=UTF-8"
},
{
"name": "date",
"value": "Mon, 10 Jul 2023 08:11:45 GMT"
},
{
"name": "vary",
"value": "Accept-Encoding"
}
],
"cookies": [],
"content": {
"size": 105,
"mimeType": "text/html",
"text": "<!doctype html><html lang=\"en-US\"><meta charset=\"utf-8\"><title>example</title></head><body></body></html>"
},
"redirectURL": "",
"headersSize": -1,
"bodySize": -1,
"_transferSize": 73,
"_error": null
},
"serverIPAddress": "10.212.184.32",
"startedDateTime": "2023-07-10T08:11:29.907Z",
"time": 15604.263000000174,
"timings": {
"blocked": 0.8870000001527951,
"dns": -1,
"ssl": -1,
"connect": -1,
"send": 0.15599999999999997,
"wait": 15599.43800000002,
"receive": 3.7820000000010623,
"_blocked_queueing": 0.7500000001527951
}
}
]
}
Expand Down

0 comments on commit aea2a6d

Please sign in to comment.