|
| 1 | +const chai = require('chai'); |
| 2 | +const expect = chai.expect; |
| 3 | +const plug = require('plug'); |
| 4 | + |
| 5 | +const http = plug('util/http.js'); |
| 6 | +const logger = plug('logger'); |
| 7 | + |
| 8 | +logger.setLogLevel('error'); |
| 9 | + |
| 10 | +describe('# http', () => { |
| 11 | + |
| 12 | + describe('# formatHeader', () => { |
| 13 | + it('null', () => { |
| 14 | + expect(http.formatHeader(null)).to.be.equal(null); |
| 15 | + }); |
| 16 | + |
| 17 | + it('empty', () => { |
| 18 | + const headers = http.formatHeader({}); |
| 19 | + expect(Object.keys(headers).length).to.be.equal(0); |
| 20 | + }); |
| 21 | + |
| 22 | + it('formatKey', () => { |
| 23 | + const headers = http.formatHeader({ |
| 24 | + 'kEY': 'value' |
| 25 | + }); |
| 26 | + expect(headers['kEY']).to.be.equal(undefined); |
| 27 | + expect(headers['Key']).to.not.be.equal(undefined); |
| 28 | + expect(headers['Key']).to.be.equal('value'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('invalidHeaderChar', () => { |
| 32 | + const headers = http.formatHeader({ |
| 33 | + 'kEY': 'value! \tg' |
| 34 | + }); |
| 35 | + expect(headers['Key']).to.be.equal('value! \tg'); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('# isHttps', () => { |
| 40 | + it('empty', () => { |
| 41 | + expect(http.isHttps()).to.be.equal(false); |
| 42 | + }); |
| 43 | + |
| 44 | + it('http:', () => { |
| 45 | + expect(http.isHttps({ |
| 46 | + REQUEST: { |
| 47 | + protocol: 'http:' |
| 48 | + } |
| 49 | + })).to.be.equal(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it('https:', () => { |
| 53 | + expect(http.isHttps({ |
| 54 | + REQUEST: { |
| 55 | + protocol: 'https:' |
| 56 | + } |
| 57 | + })).to.be.equal(true); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('#getUserIp', () => { |
| 62 | + it('empty', () => { |
| 63 | + expect(http.getUserIp()).to.be.equal(''); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + |
| 68 | + describe('#isMethodLike', () => { |
| 69 | + it('isPostLike', () => { |
| 70 | + expect(http.isPostLike('POST')).to.be.equal(true); |
| 71 | + expect(http.isPostLike('PUT')).to.be.equal(true); |
| 72 | + expect(http.isPostLike('DELETE')).to.be.equal(true); |
| 73 | + |
| 74 | + expect(http.isPostLike('GET')).to.be.equal(false); |
| 75 | + expect(http.isPostLike('HEAD')).to.be.equal(false); |
| 76 | + expect(http.isPostLike('OPTIONS')).to.be.equal(false); |
| 77 | + }); |
| 78 | + |
| 79 | + it('isGetLike ', () => { |
| 80 | + expect(http.isGetLike('POST')).to.be.equal(false); |
| 81 | + expect(http.isGetLike('PUT')).to.be.equal(false); |
| 82 | + expect(http.isGetLike('DELETE')).to.be.equal(false); |
| 83 | + |
| 84 | + expect(http.isGetLike('GET')).to.be.equal(true); |
| 85 | + expect(http.isGetLike('HEAD')).to.be.equal(true); |
| 86 | + expect(http.isGetLike('OPTIONS')).to.be.equal(true); |
| 87 | + }); |
| 88 | + }); |
| 89 | +}); |
0 commit comments