defaultnormalizer.test.js
2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
const expect = require('chai').expect;
const DefaultNormalizer = require('../lib/normalizer/default.js');
describe('default normalizer', ()=>{
let ctx = require('./fixtures/normalizer.json');
let normalizer;
beforeEach(()=>{
normalizer = new DefaultNormalizer();
});
describe('constructor()', () => {
it('should create an object', ()=>{
expect(normalizer.globalOptions).to.eql({});
normalizer = new DefaultNormalizer(ctx.opts[0]);
expect(normalizer.globalOptions).to.eql(ctx.opts[0]);
});
});
describe('normalize()', () => {
it('should compose querystring from qs ', () => {
let res = normalizer.normalize(ctx.reqs[0]);
expect(res).to.eql({
uri: 'http://www.GOOGLE.com?q=querysomething',
qs:{
q:'querysomething',
}
});
});
it('should use local options to override global options', () => {
normalizer = new DefaultNormalizer(ctx.opts[1]);
let res = normalizer.normalize(ctx.reqs[1],ctx.opts[2]);
expect(res).to.eql({
uri: 'http://www.GOOGLE.com',
}
);
});
it('should compose querystrings for get method', () => {
normalizer = new DefaultNormalizer(ctx.opts[0]);
let res = normalizer.normalize(ctx.reqs[2]);
expect(res).to.eql({
method: 'GET',
uri: 'http://www.google.com?q=querysomething&gfe_rd=cr&ei=qg5QVYyVBcrC8Afz6ICoDw&gws_rd=ssl&safe=strict',
qs:{
q: 'querysomething',gfe_rd: 'cr',ei: 'qg5QVYyVBcrC8Afz6ICoDw',gws_rd: 'ssl',safe: 'strict'
}
});
});
it('should convert object form to string body for post method', () => {
let res = normalizer.normalize(ctx.reqs[3]);
expect(res.body).to.not.an('undefined');
expect(res).to.eql({
method: 'POST',
uri: 'https://github.com/logout',
form: {
utf8: '✓',
authenticity_token: 'R1d7nfjekS+a5/h8+L2DrSy02gt7GCxRLFla5JBjwMrYQRDRrGPaTFz/tHTQKaqYfMeZIYlYMhfBrnMwDDz+cg=='
},
body: 'authenticity_token=R1d7nfjekS+a5/h8+L2DrSy02gt7GCxRLFla5JBjwMrYQRDRrGPaTFz/tHTQKaqYfMeZIYlYMhfBrnMwDDz+cg==&utf8=✓'
});
});
it('should convert string form to string body for post method', () => {
let res = normalizer.normalize(ctx.reqs[4]);
expect(res.body).to.not.an('undefined');
expect(res).to.eql({
method: 'POST',
uri: 'https://github.com/logout',
form: 'utf8=✓&authenticity_token=R1d7nfjekS+a5/h8+L2DrSy02gt7GCxRLFla5JBjwMrYQRDRrGPaTFz/tHTQKaqYfMeZIYlYMhfBrnMwDDz+cg==',
body: 'authenticity_token=R1d7nfjekS+a5/h8+L2DrSy02gt7GCxRLFla5JBjwMrYQRDRrGPaTFz/tHTQKaqYfMeZIYlYMhfBrnMwDDz+cg==&utf8=✓'
});
});
it('should normalize json body for post method', () => {
let res = normalizer.normalize(ctx.reqs[5]);
expect(res.body).to.not.an('undefined');
expect(res.body).to.eql(JSON.stringify({
authenticity_token: 'R1d7nfjekS+a5/h8+L2DrSy02gt7GCxRLFla5JBjwMrYQRDRrGPaTFz/tHTQKaqYfMeZIYlYMhfBrnMwDDz+cg==',
utf8:'✓'
}));
});
});
});