@dkx/http-server
Advanced tools
Comparing version 0.0.1 to 0.1.0
{ | ||
"name": "@dkx/http-server", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Minimalistic HTTP server", | ||
@@ -8,2 +8,5 @@ "repository": "git@gitlab.com:dkx/http/server.git", | ||
"license": "MIT", | ||
"dependencies": { | ||
"vary": "^1.1.2" | ||
}, | ||
"devDependencies": { | ||
@@ -14,2 +17,3 @@ "@types/chai": "^4.1.4", | ||
"@types/node-fetch": "^2.1.2", | ||
"@types/vary": "^1.1.0", | ||
"chai": "^4.1.2", | ||
@@ -16,0 +20,0 @@ "mocha": "^5.2.0", |
# DKX/Http/Server | ||
Super simple HTTP server with zero dependencies and with middlewares support for node.js. | ||
Super simple HTTP server with middlewares support for node.js. | ||
@@ -164,3 +164,28 @@ ## Installation | ||
`Response`: cloned `Response` object with modified status. | ||
* **`hasHeader()`** | ||
Check whether header exists. | ||
**Arguments:** | ||
`name: string`: name of HTTP header to check. | ||
**Return:** | ||
`boolean` | ||
* **`getHeader()`** | ||
Get HTTP response header. | ||
**Arguments:** | ||
`name: string`: name of HTTP header. | ||
`defaultValue: undefined|string|number|Array<string>`: default value to return if HTTP header does not exists. | ||
**Return:** | ||
`undefined|string|number|Array<string>` | ||
* **`withHeader()`** | ||
@@ -179,2 +204,14 @@ | ||
* **`withVaryHeader()`** | ||
Write vary HTTP response header. | ||
**Arguments:** | ||
`field: string|Array<string>`: name of header you wish to add into vary header | ||
**Return:** | ||
`Response`: cloned `Response` object with modified headers. | ||
* **`removeHeader()`** | ||
@@ -181,0 +218,0 @@ |
@@ -0,1 +1,2 @@ | ||
import {append as varyAppend} from 'vary'; | ||
import {ResponseBodyBag, ResponseBodyBagAttachedListener} from './response-body-bag'; | ||
@@ -48,2 +49,17 @@ | ||
public hasHeader(name: string): boolean | ||
{ | ||
return typeof this._headers[name] !== 'undefined'; | ||
} | ||
public getHeader(name: string, defaultValue?: ResponseHeader): ResponseHeader|undefined | ||
{ | ||
return this.hasHeader(name) ? | ||
this._headers[name] : | ||
defaultValue | ||
; | ||
} | ||
public withHeader(name: string, value: ResponseHeader): Response | ||
@@ -60,5 +76,19 @@ { | ||
public withVaryHeader(field: string|Array<string>): Response | ||
{ | ||
const prev: ResponseHeader = this.getHeader('Vary', ''); | ||
const header: string = Array.isArray(prev) ? prev.join(', ') : String(prev); | ||
const next = varyAppend(header, field); | ||
return next ? | ||
this.withHeader('Vary', next) : | ||
this.clone() | ||
; | ||
} | ||
public removeHeader(name: string): Response | ||
{ | ||
if (typeof this._headers[name] === 'undefined') { | ||
if (!this.hasHeader(name)) { | ||
return this.clone(); | ||
@@ -65,0 +95,0 @@ } |
@@ -6,2 +6,3 @@ import {expect} from 'chai'; | ||
let bodyBag: ResponseBodyBag; | ||
let res: Response; | ||
@@ -13,2 +14,3 @@ | ||
bodyBag = new ResponseBodyBag; | ||
res = new Response(bodyBag); | ||
}); | ||
@@ -19,3 +21,3 @@ | ||
it('should store default values', () => { | ||
const res = new Response(bodyBag, 404, 'Not found', { | ||
res = new Response(bodyBag, 404, 'Not found', { | ||
'X-Test': 'hello world', | ||
@@ -36,4 +38,2 @@ }); | ||
it('should clone response', () => { | ||
const res = new Response(bodyBag); | ||
expect(res.clone()).to.not.be.equal(res); | ||
@@ -47,3 +47,4 @@ }); | ||
it('should change status', () => { | ||
const res = new Response(bodyBag, 404, 'Not found'); | ||
res = new Response(bodyBag, 404, 'Not found'); | ||
const updated = res.withStatus(200, 'OK'); | ||
@@ -60,6 +61,37 @@ | ||
describe('hasHeader()', () => { | ||
it('should return true when header exists', () => { | ||
const updated = res.withHeader('X-Test', 'hello world'); | ||
expect(updated.hasHeader('X-Test')).to.be.equal(true); | ||
}); | ||
it('should return false when header does not exists', () => { | ||
expect(res.hasHeader('X-Test')).to.be.equal(false); | ||
}); | ||
}); | ||
describe('getHeader()', () => { | ||
it('should return undefined when header does not exists', () => { | ||
expect(res.getHeader('X-Test')).to.be.equal(undefined); | ||
}); | ||
it('should return header', () => { | ||
const updated = res.withHeader('X-Test', 'hello world'); | ||
expect(updated.getHeader('X-Test')).to.be.equal('hello world'); | ||
}); | ||
it('should return default value when header does not exists', () => { | ||
expect(res.getHeader('X-Test', 'hello world')).to.be.equal('hello world'); | ||
}); | ||
}); | ||
describe('withHeader()', () => { | ||
it('should change header', () => { | ||
const res = new Response(bodyBag); | ||
const updated = res.withHeader('X-Test', 'hello'); | ||
@@ -76,6 +108,37 @@ | ||
describe('withVaryHeader()', () => { | ||
it('should add new vary header', () => { | ||
const updated = res.withVaryHeader('Accept'); | ||
expect(updated.headers).to.be.eql({ | ||
Vary: 'Accept', | ||
}); | ||
}); | ||
it('should append vary header to existing string vary header', () => { | ||
const updated = res | ||
.withHeader('Vary', 'Accept-Encoding') | ||
.withVaryHeader('Accept'); | ||
expect(updated.headers).to.be.eql({ | ||
Vary: 'Accept-Encoding, Accept', | ||
}); | ||
}); | ||
it('should append vary header to existing array vary header', () => { | ||
const updated = res | ||
.withHeader('Vary', ['Accept-Encoding']) | ||
.withVaryHeader('Accept'); | ||
expect(updated.headers).to.be.eql({ | ||
Vary: 'Accept-Encoding, Accept', | ||
}); | ||
}); | ||
}); | ||
describe('removeHeader()', () => { | ||
it('should just clone response when header does not exists', () => { | ||
const res = new Response(bodyBag); | ||
const updated = res.removeHeader('X-Test'); | ||
@@ -89,3 +152,3 @@ | ||
it('should remove header', () => { | ||
const res = new Response(bodyBag, 200, 'OK', { | ||
res = new Response(bodyBag, 200, 'OK', { | ||
'X-Test': 'hello', | ||
@@ -92,0 +155,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25298
600
238
1
10
+ Addedvary@^1.1.2
+ Addedvary@1.1.2(transitive)