Skip to content

Commit

Permalink
Merge pull request #7 from Idrinth/coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Idrinth authored Jul 17, 2020
2 parents 0b1dcb6 + ff811c9 commit 91a4c93
Show file tree
Hide file tree
Showing 9 changed files with 71 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
17 changes: 17 additions & 0 deletions test/middlewares/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
expect,
} from 'chai';
import 'mocha';
import { Request } from '../../src/request';
import { Result } from '../../src/result';

describe('middlewares/cookie', () => {
it('should be a class', () => {
Expand All @@ -14,4 +16,19 @@ describe('middlewares/cookie', () => {
it('should have a static method process', () => {
expect(Cookie.process,).to.be.a('function',);
},);
it('should not set cookies by default', () => {
expect(Cookie.prepare(<Request>{}),).to.deep.equal({cookies: {}},);
},);
it('should get cookies by default', () => {
expect(() => Cookie.process(<Result><unknown>{response: {cookies: {abc: 'def'}}}),).to.not.throw();
},);
it('should set cookies it has', () => {
expect(Cookie.prepare(<Request>{}),).to.deep.equal({cookies: {abc: 'def'}},);
},);
it('should change no cookies if there are none', () => {
expect(() => Cookie.process(<Result><unknown>{response: {cookies: {}}}),).to.not.throw();
},);
it('should set cookies it has', () => {
expect(Cookie.prepare(<Request>{}),).to.deep.equal({cookies: {abc: 'def'}},);
},);
},);
17 changes: 17 additions & 0 deletions test/middlewares/csrf-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
expect,
} from 'chai';
import 'mocha';
import { Request } from '../../src/request';
import { Result } from '../../src/result';

describe('middlewares/csrf-header', () => {
it('should be a class', () => {
Expand All @@ -14,4 +16,19 @@ describe('middlewares/csrf-header', () => {
it('should have a static method process', () => {
expect(CsrfHeader.process,).to.be.a('function',);
},);
it('should not set token by default', () => {
expect(CsrfHeader.prepare(<Request>{}),).to.deep.equal({headers: {}},);
},);
it('should get token by default', () => {
expect(() => CsrfHeader.process(<Result><unknown>{response: {headers: {'x-csrf-token': 'def'}}}),).to.not.throw();
},);
it('should set token it has', () => {
expect(CsrfHeader.prepare(<Request>{}),).to.deep.equal({headers: {'x-csrf-token': 'def'}},);
},);
it('should change no token if there\'s none', () => {
expect(() => CsrfHeader.process(<Result><unknown>{response: {headers: {}}}),).to.not.throw();
},);
it('should set token it has', () => {
expect(CsrfHeader.prepare(<Request>{}),).to.deep.equal({headers: {'x-csrf-token': 'def'}},);
},);
},);
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 91a4c93

Please sign in to comment.