From cb5ca04dadbfc060cacb9cc5e26874528df3ce45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Fri, 17 Jul 2020 19:13:44 +0200 Subject: [PATCH 1/2] testing cookie and csrf-header middlewres --- test/middlewares/cookie.ts | 17 +++++++++++++++++ test/middlewares/csrf-header.ts | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/test/middlewares/cookie.ts b/test/middlewares/cookie.ts index d2b7596e8..db930b462 100644 --- a/test/middlewares/cookie.ts +++ b/test/middlewares/cookie.ts @@ -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', () => { @@ -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({}),).to.deep.equal({cookies: {}},); + },); + it('should get cookies by default', () => { + expect(() => Cookie.process({response: {cookies: {abc: 'def'}}}),).to.not.throw(); + },); + it('should set cookies it has', () => { + expect(Cookie.prepare({}),).to.deep.equal({cookies: {abc: 'def'}},); + },); + it('should change no cookies if there are none', () => { + expect(() => Cookie.process({response: {cookies: {}}}),).to.not.throw(); + },); + it('should set cookies it has', () => { + expect(Cookie.prepare({}),).to.deep.equal({cookies: {abc: 'def'}},); + },); },); diff --git a/test/middlewares/csrf-header.ts b/test/middlewares/csrf-header.ts index e0a3a0f45..4bf3b0bf2 100644 --- a/test/middlewares/csrf-header.ts +++ b/test/middlewares/csrf-header.ts @@ -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', () => { @@ -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({}),).to.deep.equal({headers: {}},); + },); + it('should get token by default', () => { + expect(() => CsrfHeader.process({response: {headers: {'x-csrf-token': 'def'}}}),).to.not.throw(); + },); + it('should set token it has', () => { + expect(CsrfHeader.prepare({}),).to.deep.equal({headers: {'x-csrf-token': 'def'}},); + },); + it('should change no token if there\'s none', () => { + expect(() => CsrfHeader.process({response: {headers: {}}}),).to.not.throw(); + },); + it('should set token it has', () => { + expect(CsrfHeader.prepare({}),).to.deep.equal({headers: {'x-csrf-token': 'def'}},); + },); },); From ff811c9f5032b592c51956b2501102566d262940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Fri, 17 Jul 2020 19:50:50 +0200 Subject: [PATCH 2/2] testing user agent testing encoding replacing outdated types of form-urlencoded --- package-lock.json | 5 ----- package.json | 1 - src/middlewares/encoding.ts | 4 ++-- test/middlewares/encoding.ts | 13 +++++++++++++ test/middlewares/user-agent.ts | 4 ++++ tsconfig.json | 6 +++++- typings/form-urlencoded.d.ts | 13 +++++++++++++ 7 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 typings/form-urlencoded.d.ts diff --git a/package-lock.json b/package-lock.json index e11eb297b..5566198e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -412,11 +412,6 @@ "integrity": "sha512-OCutwjDZ4aFS6PB1UZ988C4YgwlBHJd6wCeQqaLdmadZ/7e+w79+hbMUFC1QXDNCmdyoRfAFdm0RypzwR+Qpag==", "dev": true }, - "@types/form-urlencoded": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/@types/form-urlencoded/-/form-urlencoded-2.0.1.tgz", - "integrity": "sha512-Kz3LwezuNvsQF8ZcswMv4b5ZhKmjTkH2Rxle6l/520Npvaxho6b4HN9lStp6HhGrZsL/rwJHpWK9vM1RA7uZbw==" - }, "@types/json-schema": { "version": "7.0.5", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.5.tgz", diff --git a/package.json b/package.json index 42ce920f1..4afc4e276 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/middlewares/encoding.ts b/src/middlewares/encoding.ts index c048e91ce..9fb6567b7 100644 --- a/src/middlewares/encoding.ts +++ b/src/middlewares/encoding.ts @@ -4,7 +4,7 @@ import { import { Request, } from '../request'; -import * as formurlencoded from 'form-urlencoded'; +import formUrlEncoded from 'form-urlencoded'; import { Result, } from '../result'; @@ -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; } diff --git a/test/middlewares/encoding.ts b/test/middlewares/encoding.ts index ca7f715ac..e2d5e0c3b 100644 --- a/test/middlewares/encoding.ts +++ b/test/middlewares/encoding.ts @@ -3,6 +3,7 @@ import { expect, } from 'chai'; import 'mocha'; +import { Request } from '../../src/request'; describe('middlewares/encoding', () => { it('should be a class', () => { @@ -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({body: {a: 'b'}, autohandle: 'json'}).body,).to.equal('{"a":"b"}',); + },); + it('prepare should json-transform array to string', () => { + expect(Encoding.prepare({body: ['a'], autohandle: 'json'}).body,).to.equal('["a"]',); + },); + it('prepare should form-transform object to string', () => { + expect(Encoding.prepare({body: {a: 'b'}, autohandle: 'form'}).body,).to.equal('a=b',); + },); + it('prepare should not transform by default', () => { + expect(Encoding.prepare({body: {}}).body,).to.deep.equal({},); + },); },); diff --git a/test/middlewares/user-agent.ts b/test/middlewares/user-agent.ts index 732af1b51..71b1f308e 100644 --- a/test/middlewares/user-agent.ts +++ b/test/middlewares/user-agent.ts @@ -3,6 +3,7 @@ import { expect, } from 'chai'; import 'mocha'; +import { Request } from '../../src/request'; describe('middlewares/user-agent', () => { it('should be a class', () => { @@ -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({}).headers['user-agent'],).to.be.a('string',); + },); },); diff --git a/tsconfig.json b/tsconfig.json index 504cd646e..21e4d030e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,9 @@ { "compilerOptions": { "experimentalDecorators": true - } + }, + "typeRoots" : [ + "./typings", + "@types" + ] } diff --git a/typings/form-urlencoded.d.ts b/typings/form-urlencoded.d.ts new file mode 100644 index 000000000..2eedc8108 --- /dev/null +++ b/typings/form-urlencoded.d.ts @@ -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; +} \ No newline at end of file