Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
fix(converter): set content type from file extension (#3)
Browse files Browse the repository at this point in the history
closes #2
  • Loading branch information
derevnjuk authored Jul 2, 2020
1 parent dd9224c commit 02d5b91
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"homepage": "https://github.com/NeuraLegion/postman2har#readme",
"dependencies": {
"faker": "^4.1.0",
"mime-types": "^2.1.27",
"semver": "^7.3.2",
"tslib": "~1.11.1"
},
Expand All @@ -67,6 +68,7 @@
"@types/debug": "^4.1.5",
"@types/faker": "^4.1.12",
"@types/har-format": "^1.2.4",
"@types/mime-types": "^2.1.0",
"@types/mocha": "~7.0.2",
"@types/node": "~14.0.13",
"@types/semver": "^7.3.1",
Expand Down
32 changes: 24 additions & 8 deletions src/converter/DefaultConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { Converter } from './Converter';
import { Validator } from '../validator';
import { VariableParser, VariableParserFactory } from '../parser';
import Har from 'har-format';
import { lookup } from 'mime-types';
import { ok } from 'assert';
import { format, parse, UrlObject } from 'url';
import { parse as parseQS, ParsedUrlQuery, stringify } from 'querystring';
import { basename, extname } from 'path';

enum AuthLocation {
QUERY = 'queryString',
Expand Down Expand Up @@ -83,7 +85,7 @@ export class DefaultConverter implements Converter {
const request: Har.Request = {
url,
method: (method ?? 'GET').toUpperCase(),
headers: this.convertHeaders(header!, variables),
headers: this.convertHeaders(header ?? '', variables),
queryString: this.convertQuery(url, variables),
cookies: [],
postData: body && this.convertBody(body, variables),
Expand Down Expand Up @@ -308,11 +310,25 @@ export class DefaultConverter implements Converter {
return {
mimeType: 'multipart/form-data',
params: Array.isArray(body.formdata)
? body.formdata.map((x: Postman.FormParam) => ({
name: parser.parse(x.key ?? ''),
value: parser.parse(x.value ?? ''),
contentType: x.contentType
}))
? body.formdata.map((x: Postman.FormParam) => {
const fileName: string | undefined = x.src
? basename(Array.isArray(x.src) ? x.src.pop()! : x.src)
: undefined;

const extension: string | undefined = fileName
? extname(fileName)
: fileName;

const contentType: string | undefined =
x.contentType ?? (lookup(extension ?? '') || undefined);

return {
fileName,
contentType,
name: parser.parse(x.key ?? ''),
value: parser.parse(x.value ?? '')
};
})
: [],
text: ''
};
Expand All @@ -330,7 +346,7 @@ export class DefaultConverter implements Converter {
value: parser.parse(x.value ?? '')
}));
} else {
params = Object.entries(parseQS(body.urlencoded!)).map(
params = Object.entries(parseQS(body.urlencoded ?? '')).map(
([name, value]) => ({
name,
value: Array.isArray(value) ? value.join('&') : value
Expand All @@ -343,7 +359,7 @@ export class DefaultConverter implements Converter {
? body.urlencoded
: stringify(
Object.fromEntries(
body.urlencoded!.map((x: Postman.QueryParam) => [
(body.urlencoded ?? []).map((x: Postman.QueryParam) => [
parser.parse(x.key ?? ''),
parser.parse(x.value ?? '')
])
Expand Down

0 comments on commit 02d5b91

Please sign in to comment.