@@ -12,29 +12,32 @@ CliReporter = require '../../src/cli-reporter'
1212
1313describe ' 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