UNPKG

2.42 kBJavaScriptView Raw
1'use strict';
2
3const assert = require('assert');
4const Response = require('../lib/response');
5
6describe('Response', () => {
7 it('creates response from static factory', () => {
8 const properties = {
9 url: 'https://www.example.com',
10 body: 'body content',
11 statusCode: 200
12 };
13
14 const response = Response.create(properties);
15 assert.equal(response.url, properties.url);
16 assert.equal(response.statusCode, properties.statusCode);
17 assert.equal(response.body, properties.body);
18 });
19
20 it('gets a header', () => {
21 const response = Response.create();
22 response.headers = {
23 a: 1,
24 b: 2
25 };
26
27 assert.equal(response.getHeader('a'), 1);
28 });
29
30 it('sets a header', () => {
31 const response = Response.create();
32 response.addHeader('a', 1);
33
34 assert.equal(response.getHeader('a'), 1);
35 });
36
37 it('sets headers from an object', () => {
38 const response = Response.create();
39 response.addHeader({
40 a: 1,
41 b: 2
42 });
43
44 assert.equal(response.getHeader('a'), 1);
45 assert.equal(response.getHeader('b'), 2);
46 });
47
48 it('sets a multiple headers', () => {
49 const response = Response.create();
50 const headers = {
51 a: 1,
52 b: 2
53 };
54 response.addHeader(headers).addHeader('c', 3);
55
56 assert.equal(response.getHeader('a'), 1);
57 assert.equal(response.getHeader('b'), 2);
58 assert.equal(response.getHeader('c'), 3);
59 });
60
61 it('returns the body length using the Content-Length header', () => {
62 const response = Response.create();
63 response.headers = {
64 'Content-Length': 7
65 };
66
67 assert.equal(response.length, 7);
68 });
69
70 it('calculates the length of a text body', () => {
71 const content = 'xxxxxxxxxx';
72 const response = Response.create();
73 response.body = content;
74
75 assert.equal(response.length, content.length);
76 });
77
78 it('calculates the length of a json body', () => {
79 const response = Response.create();
80 response.body = {
81 x: 1,
82 y: 2,
83 z: 3
84 };
85
86 assert.equal(response.length, 19);
87 });
88
89 it('serialises required state', () => {
90 const state = {
91 url: 'https://www.example.com',
92 body: 'body content',
93 statusCode: 200,
94 elapsedTime: 10
95 };
96
97 const response = Response.create(state);
98
99 const asString = JSON.stringify(response);
100 const asJson = JSON.parse(asString);
101 assert.deepEqual(asJson, state);
102 });
103});