Skip to content

Commit

Permalink
testing user agent
Browse files Browse the repository at this point in the history
testing encoding
replacing outdated types of form-urlencoded
  • Loading branch information
Idrinth committed Jul 17, 2020
1 parent cb5ca04 commit ff811c9
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 9 deletions.
5 changes: 0 additions & 5 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"needle": "^2.5",
"@types/needle": "^2.0.4",
"form-urlencoded": "^4.1.4",
"@types/form-urlencoded": "^2.0.1",
"app-root-path": "^3",
"@types/app-root-path": "^1.2.4",
"cli-table3": "^0.6",
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
import {
Request,
} from '../request';
import * as formurlencoded from 'form-urlencoded';
import formUrlEncoded from 'form-urlencoded';
import {
Result,
} from '../result';
Expand All @@ -18,7 +18,7 @@ class Encoding {
return request;
}
if (request.autohandle === 'form' && typeof request.body === 'object') {
request.body = formurlencoded(request.body,);
request.body = formUrlEncoded(request.body,);
}
return request;
}
Expand Down
13 changes: 13 additions & 0 deletions test/middlewares/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
expect,
} from 'chai';
import 'mocha';
import { Request } from '../../src/request';

describe('middlewares/encoding', () => {
it('should be a class', () => {
Expand All @@ -14,4 +15,16 @@ describe('middlewares/encoding', () => {
it('should have a static method process', () => {
expect(Encoding.process,).to.be.a('function',);
},);
it('prepare should json-transform object to string', () => {
expect(Encoding.prepare(<Request><unknown>{body: {a: 'b'}, autohandle: 'json'}).body,).to.equal('{"a":"b"}',);
},);
it('prepare should json-transform array to string', () => {
expect(Encoding.prepare(<Request><unknown>{body: ['a'], autohandle: 'json'}).body,).to.equal('["a"]',);
},);
it('prepare should form-transform object to string', () => {
expect(Encoding.prepare(<Request><unknown>{body: {a: 'b'}, autohandle: 'form'}).body,).to.equal('a=b',);
},);
it('prepare should not transform by default', () => {
expect(Encoding.prepare(<Request><unknown>{body: {}}).body,).to.deep.equal({},);
},);
},);
4 changes: 4 additions & 0 deletions test/middlewares/user-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
expect,
} from 'chai';
import 'mocha';
import { Request } from '../../src/request';

describe('middlewares/user-agent', () => {
it('should be a class', () => {
Expand All @@ -14,4 +15,7 @@ describe('middlewares/user-agent', () => {
it('should have a static method process', () => {
expect(UserAgent.process,).to.be.a('function',);
},);
it('prepare should add a user agent header', () => {
expect(UserAgent.prepare(<Request>{}).headers['user-agent'],).to.be.a('string',);
},);
},);
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"compilerOptions": {
"experimentalDecorators": true
}
},
"typeRoots" : [
"./typings",
"@types"
]
}
13 changes: 13 additions & 0 deletions typings/form-urlencoded.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Type definitions for form-urlencoded 4
// Project: https://github.com/iambumblehead/form-urlencoded#readme
declare module 'form-urlencoded' {
interface FormEncodedOptions {
sorted?: boolean;
skipIndex?: boolean;
ignorenull?: boolean;
skipBracket?: boolean;
useDot?: boolean;
}

export default function(data: any, opts?: FormEncodedOptions): string;
}

0 comments on commit ff811c9

Please sign in to comment.