Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit faa1b8f

Browse files
author
Adam Kliment
committed
Added tests for content-length headers setting
1 parent aded788 commit faa1b8f

File tree

3 files changed

+103
-42
lines changed

3 files changed

+103
-42
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dredd",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "API Blueprint testing tool",
55
"main": "lib/dredd.js",
66
"bin": {

src/execute-transaction.coffee

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ executeTransaction = (transaction, callback) ->
5252
" ("+ system + ")"
5353

5454
# Add length of body if no Content-Length present
55-
if flatHeaders['Content-Length'] == undefined and request['body'] != ''
55+
caseInsensitiveMap = {}
56+
for key, value of flatHeaders
57+
caseInsensitiveMap[key.toLowerCase()] = key
58+
59+
if caseInsensitiveMap['content-length'] == undefined and request['body'] != ''
5660
flatHeaders['Content-Length'] = request['body'].length
5761

5862
if configuration.request?.headers?
@@ -85,7 +89,7 @@ executeTransaction = (transaction, callback) ->
8589
buffer = buffer + chunk
8690

8791
req.on 'error', (error) ->
88-
return callback error
92+
return callback error, req, res
8993

9094
res.on 'end', () ->
9195
real =
@@ -100,19 +104,19 @@ executeTransaction = (transaction, callback) ->
100104
statusCode: response['status']
101105

102106
gavel.isValid real, expected, 'response', (error, isValid) ->
103-
return callback error if error
107+
return callback error, req, res if error
104108

105109
if isValid
106110
test =
107111
status: "pass",
108112
title: options['method'] + ' ' + options['path']
109113
message: description
110114
configuration.reporter.addTest test, (error) ->
111-
return callback error if error
112-
return callback()
115+
return callback error, req, res if error
116+
return callback(undefined, req, res)
113117
else
114118
gavel.validate real, expected, 'response', (error, result) ->
115-
return callback(error) if error
119+
return callback(error, req, res) if error
116120
message = ''
117121
for entity, data of result
118122
for entityResult in data['results']
@@ -125,8 +129,8 @@ executeTransaction = (transaction, callback) ->
125129
expected: prettify expected
126130
request: options
127131
configuration.reporter.addTest test, (error) ->
128-
return callback error if error
129-
return callback()
132+
return callback error, req, res if error
133+
return callback(undefined, req, res)
130134

131135
if configuration.server.startsWith 'https'
132136
req = https.request options, handleRequest

test/unit/execute-transaction-test.coffee

Lines changed: 90 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,32 @@ CliReporter = require '../../src/cli-reporter'
1212

1313
describe 'executeTransaction(transaction, callback)', () ->
1414
transaction =
15-
request:
16-
body: "{\n \"type\": \"bulldozer\",\n \"name\": \"willy\"}\n"
17-
headers:
18-
"Content-Type":
19-
value: "application/json"
20-
uri: "/machines",
21-
method: "POST"
22-
response:
23-
body: "{\n \"type\": \"bulldozer\",\n \"name\": \"willy\",\n \"id\": \"5229c6e8e4b0bd7dbb07e29c\"\n}\n"
24-
headers:
25-
"content-type":
26-
value: "application/json"
27-
status: "202"
28-
origin:
29-
resourceGroupName: "Group Machine"
30-
resourceName: "Machine"
31-
actionNames: "Delete Message"
32-
exampleName: "Bogus example name"
33-
configuration:
34-
server: 'http://localhost:3000'
35-
options: []
15+
3616

3717
beforeEach () ->
18+
transaction =
19+
request:
20+
body: "{\n \"type\": \"bulldozer\",\n \"name\": \"willy\"}\n"
21+
headers:
22+
"Content-Type":
23+
value: "application/json"
24+
uri: "/machines",
25+
method: "POST"
26+
response:
27+
body: "{\n \"type\": \"bulldozer\",\n \"name\": \"willy\",\n \"id\": \"5229c6e8e4b0bd7dbb07e29c\"\n}\n"
28+
headers:
29+
"content-type":
30+
value: "application/json"
31+
status: "202"
32+
origin:
33+
resourceGroupName: "Group Machine"
34+
resourceName: "Machine"
35+
actionNames: "Delete Message"
36+
exampleName: "Bogus example name"
37+
configuration:
38+
server: 'http://localhost:3000'
39+
options: []
40+
3841
nock.disableNetConnect()
3942

4043
afterEach () ->
@@ -45,7 +48,63 @@ describe 'executeTransaction(transaction, callback)', () ->
4548
data = {}
4649
server = {}
4750

48-
describe 'backend responds as it should', () ->
51+
describe 'setting of content-length header', () ->
52+
describe 'when content-length header is not present in the specified request', () ->
53+
beforeEach () ->
54+
server = nock('http://localhost:3000').
55+
post('/machines', {"type":"bulldozer","name":"willy"}).
56+
reply transaction['response']['status'],
57+
transaction['response']['body'],
58+
{'Content-Type': 'application/json'}
59+
transaction['configuration'].reporter = new CliReporter()
60+
nock.recorder.rec(true)
61+
62+
it 'should send content length header ', (done) ->
63+
executeTransaction transaction, (error, req, res) ->
64+
assert.isDefined req._headers['content-length']
65+
done()
66+
67+
it 'sent content-length header should have proper value', (done) ->
68+
executeTransaction transaction, (error, req, res) ->
69+
assert.equal req._headers['content-length'], 44
70+
done()
71+
72+
73+
describe 'when content-length header is present in the specified request', () ->
74+
beforeEach () ->
75+
transaction['configuration'].reporter = new CliReporter()
76+
77+
server = nock('http://localhost:3000').
78+
post('/machines', {"type":"bulldozer","name":"willy"}).
79+
reply transaction['response']['status'],
80+
transaction['response']['body'],
81+
{'Content-Type': 'application/json'}
82+
83+
it 'should not overwrite specified value', (done) ->
84+
transaction['request']['headers']['Content-Length'] = {}
85+
transaction['request']['headers']['Content-Length']['value'] = '333'
86+
executeTransaction transaction, (error, req, res) ->
87+
assert.equal req._headers['content-length'], '333'
88+
done()
89+
90+
it 'matching of content-length header in specification should be case insensitive', (done) ->
91+
transaction['request']['headers']['CONTENT-LENGTH'] = {}
92+
transaction['request']['headers']['CONTENT-LENGTH']['value'] = '777'
93+
executeTransaction transaction, (error, req, res) ->
94+
assert.equal req._headers['content-length'], '777'
95+
done()
96+
97+
98+
it 'should not add another content-length header to the real request', (done) ->
99+
transaction['request']['headers']['content-length'] = {}
100+
transaction['request']['headers']['content-length']['value'] = '445'
101+
executeTransaction transaction, (error, req, res) ->
102+
assert.equal req._headers['content-length'], '445'
103+
done()
104+
105+
106+
107+
describe 'when backend responds as it should', () ->
49108
beforeEach () ->
50109
server = nock('http://localhost:3000').
51110
post('/machines', {"type":"bulldozer","name":"willy"}).
@@ -65,8 +124,9 @@ describe 'executeTransaction(transaction, callback)', () ->
65124
assert.notOk error
66125
done()
67126

68-
describe 'backend responds with non valid response', () ->
127+
describe 'when backend responds with non valid response', () ->
69128
beforeEach () ->
129+
transaction['configuration'].reporter = new CliReporter()
70130
server = nock('http://localhost:3000').
71131
post('/machines', {"type":"bulldozer","name":"willy"}).
72132
reply transaction['response']['status'],
@@ -81,6 +141,7 @@ describe 'executeTransaction(transaction, callback)', () ->
81141

82142
describe 'when there are global headers in the configuration', () ->
83143
beforeEach () ->
144+
transaction['configuration'].reporter = new CliReporter()
84145
server = nock('http://localhost:3000').
85146
post('/machines', {"type":"bulldozer","name":"willy"}).
86147
matchHeader('X-Header', 'foo').
@@ -99,6 +160,7 @@ describe 'executeTransaction(transaction, callback)', () ->
99160

100161
describe 'when server uses https', () ->
101162
beforeEach () ->
163+
transaction['configuration'].reporter = new CliReporter()
102164
server = nock('https://localhost:3000').
103165
post('/machines', {"type":"bulldozer","name":"willy"}).
104166
reply transaction['response']['status'],
@@ -113,30 +175,25 @@ describe 'executeTransaction(transaction, callback)', () ->
113175

114176
describe 'when server responds with html', () ->
115177
beforeEach () ->
116-
nock('https://localhost:3000').
178+
transaction['configuration'].reporter = new CliReporter()
179+
nock('http://localhost:3000').
117180
post('/machines', {"type":"bulldozer","name":"willy"}).
118181
reply transaction['response']['status'],
119182
transaction['response']['body'],
120-
{'Content-Type': 'application/json'}
183+
{'Content-Type': 'text/html'}
121184
sinon.spy htmlStub, 'prettyPrint'
122-
transaction.response.headers =
123-
"content-type":
124-
value: "text/html"
125185

126186
afterEach () ->
127187
htmlStub.prettyPrint.restore()
128-
transaction.response.headers =
129-
"content-type":
130-
value: "application/json"
131188

132189
it 'should prettify the html for reporting', (done) ->
133190
executeTransaction transaction, () ->
134191
assert.ok htmlStub.prettyPrint.called
135192
done()
136193

137-
138194
describe 'when dry run', () ->
139-
before () ->
195+
beforeEach () ->
196+
transaction['configuration'].reporter = new CliReporter()
140197
transaction['configuration']['options'] = {'dry-run' : true}
141198
server = nock('http://localhost:3000').
142199
post('/machines', {"type":"bulldozer","name":"willy"}).

0 commit comments

Comments
 (0)