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

Commit 0c09fee

Browse files
author
Paulo Silva
committed
unit test implementation
1 parent d5a3f9f commit 0c09fee

File tree

2 files changed

+116
-39
lines changed

2 files changed

+116
-39
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
HTTP/1.1 304 Not Modified
2+
Content-Type: application/json
3+
Date: Sun, 21 Jul 2013 13:23:55 GMT
4+
X-Apiary-Ratelimit-Limit: 120
5+
X-Apiary-Ratelimit-Remaining: 119
6+
Content-Length: 119
7+
Connection: keep-alive
8+
9+
{ "message": "hello world" }

test/unit/parser-test.coffee

Lines changed: 107 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ fs = require 'fs'
44
parser = require '../../src/parser'
55

66
describe "parser module", () ->
7-
7+
88
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
99
describe "parseHeaders(headersLines)", () ->
1010
it "should be a function", () ->
1111
assert.isFunction parser.parseHeaders
12-
12+
1313
describe "its return", () ->
1414
output = ""
1515
headerLines = [
@@ -19,14 +19,14 @@ describe "parser module", () ->
1919
"Content-Type: application/json",
2020
"Content-Length: 39",
2121
]
22-
22+
2323
before () ->
2424
output = parser.parseHeaders headerLines
2525

2626
describe "its retrun", () ->
2727
it "should be object", () ->
2828
assert.isObject output
29-
29+
3030
['User-Agent', 'Host', "Accept", "Content-Type", "Content-Length"].forEach (key) ->
3131
it "should contain key '" + key + "'", () ->
3232
assert.include Object.keys(output), key
@@ -43,26 +43,26 @@ describe "parser module", () ->
4343
lineStrings =
4444
POST: "POST /shopping-cart HTTP/1.1"
4545
GET: "GET /shopping-cart HTTP/1.1"
46-
46+
4747
for method, line of lineStrings
4848
describe "return for " + method + " line", () ->
4949
output = ""
5050
before () ->
5151
output = parser.parseRequestLine line
52-
53-
it "should be object", () ->
52+
53+
it "should be object", () ->
5454
assert.isObject output
5555

5656
['method','uri','protocol'].forEach (key) ->
5757
it "should contain not empty string on key: " + key, () ->
5858
assert.isString output[key]
59-
59+
6060
it "should have parsed method " + method, () ->
6161
assert.equal output['method'], method
6262

6363
it "should have parsed uri " + method, () ->
6464
assert.equal output['uri'], "/shopping-cart"
65-
65+
6666
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1
6767
describe "parseStatusLine", () ->
6868
it "is a function", () ->
@@ -74,22 +74,22 @@ describe "parser module", () ->
7474

7575
before () ->
7676
output = parser.parseStatusLine statusLine
77-
77+
7878
['protocol','statusCode','statusMessage'].forEach (key) ->
7979
it "should contain not empty string on key: " + key, () ->
8080
assert.isString output[key]
81-
81+
8282
it 'should contain statusCode "201"', () ->
8383
assert output['statusCode'], "201"
8484

8585
it 'should contain statusMessage "Created"', () ->
8686
assert output['statusCode'], "Created"
87-
87+
8888
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
8989
describe "parseRequest(requestString)", () ->
9090
requestPath = "/../fixtures/post/request-string"
9191
requestString = ""
92-
92+
9393
before (done) ->
9494
#load fixture
9595
fs.readFile __dirname + requestPath, (err, data) ->
@@ -99,54 +99,54 @@ describe "parser module", () ->
9999

100100
it "is a function", () ->
101101
assert.isFunction parser.parseRequest
102-
102+
103103
describe 'its return', () ->
104104
output = ""
105-
105+
106106
before () ->
107107
output = parser.parseRequest(requestString)
108108

109109
['method', 'uri', 'headers', 'body'].forEach (key) ->
110110
it 'should have key "'+ key + '"', () ->
111111
assert.include Object.keys(output), key
112-
112+
113113
describe "method", () ->
114114
subject = ""
115-
115+
116116
before () ->
117117
subject = output['method']
118-
118+
119119
it 'should contain "POST"', () ->
120120
assert.equal subject, "POST"
121121

122122
describe "uri", () ->
123123
subject = ""
124-
124+
125125
before () ->
126126
subject = output['uri']
127-
127+
128128
it 'should contain "/shopping-cart"', () ->
129129
assert.equal subject, "/shopping-cart"
130130

131131
describe "headers", () ->
132132
subject = ""
133-
133+
134134
before () ->
135135
subject = output['headers']
136-
136+
137137
it 'should be object', () ->
138138
assert.isObject subject
139139

140-
it 'should have "User-Agent" key', () ->
140+
it 'should have "User-Agent" key', () ->
141141
assert.include Object.keys(subject), "User-Agent"
142-
142+
143143
it 'should have proper User-Agent value', () ->
144144
agentString = "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5"
145145
assert.equal agentString, subject['User-Agent']
146146

147147
describe "body", () ->
148148
subject = ""
149-
149+
150150
before () ->
151151
subject = output['body']
152152

@@ -158,7 +158,7 @@ describe "parser module", () ->
158158
describe "parseResponse(responseString)", () ->
159159
responsePath = "/../fixtures/post/response-string"
160160
responseString = ""
161-
161+
162162
before (done) ->
163163
#load fixture
164164
fs.readFile __dirname + responsePath, (err, data) ->
@@ -168,58 +168,126 @@ describe "parser module", () ->
168168

169169
it "is a function", () ->
170170
assert.isFunction parser.parseResponse
171-
171+
172172
describe 'its return', () ->
173173
output = ""
174-
174+
175175
before () ->
176176
output = parser.parseResponse(responseString)
177177

178178
['statusCode', 'statusMessage', 'headers', 'body'].forEach (key) ->
179179
it 'should have key "'+ key + '"', () ->
180180
assert.include Object.keys(output), key
181-
181+
182182
describe "statusCode", () ->
183183
subject = ""
184-
184+
185185
before () ->
186186
subject = output['statusCode']
187-
187+
188188
it 'should contain "201"', () ->
189189
assert.equal subject, "201"
190190

191191
describe "statusMessage", () ->
192192
subject = ""
193-
193+
194194
before () ->
195195
subject = output['statusMessage']
196-
196+
197197
it 'should contain "Created"', () ->
198198
assert.equal subject, "Created"
199199

200200
describe "headers", () ->
201201
subject = ""
202-
202+
203203
before () ->
204204
subject = output['headers']
205-
205+
206206
it 'should be object', () ->
207207
assert.isObject subject
208208

209-
it 'should have "Content-Type" key', () ->
209+
it 'should have "Content-Type" key', () ->
210210
assert.include Object.keys(subject), "Content-Type"
211-
211+
212212
it 'should have proper Content-Type value', () ->
213213
agentString = "application/json"
214214
assert.equal agentString, subject['Content-Type']
215215

216216
describe "body", () ->
217217
subject = ""
218-
218+
219219
before () ->
220220
subject = output['body']
221221

222222
it 'should contain proper body string', () ->
223223
expectedBody = '{ "status": "created", "url": "/shopping-cart/2" }'
224224
assert.equal expectedBody, subject
225-
225+
226+
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6
227+
describe "parseResponse(responseString) (w/ multi word status message)", () ->
228+
responsePath = "/../fixtures/get/response-string-304"
229+
responseString = ""
230+
231+
before (done) ->
232+
#load fixture
233+
fs.readFile __dirname + responsePath, (err, data) ->
234+
done err if err
235+
responseString = data.toString()
236+
done()
237+
238+
it "is a function", () ->
239+
assert.isFunction parser.parseResponse
240+
241+
describe 'its return', () ->
242+
output = ""
243+
244+
before () ->
245+
output = parser.parseResponse(responseString)
246+
247+
['statusCode', 'statusMessage', 'headers', 'body'].forEach (key) ->
248+
it 'should have key "'+ key + '"', () ->
249+
assert.include Object.keys(output), key
250+
251+
describe "statusCode", () ->
252+
subject = ""
253+
254+
before () ->
255+
subject = output['statusCode']
256+
257+
it 'should contain "304"', () ->
258+
assert.equal subject, "304"
259+
260+
describe "statusMessage", () ->
261+
subject = ""
262+
263+
before () ->
264+
subject = output['statusMessage']
265+
266+
it 'should contain "Not Modified"', () ->
267+
assert.equal subject, "Not Modified"
268+
269+
describe "headers", () ->
270+
subject = ""
271+
272+
before () ->
273+
subject = output['headers']
274+
275+
it 'should be object', () ->
276+
assert.isObject subject
277+
278+
it 'should have "Content-Type" key', () ->
279+
assert.include Object.keys(subject), "Content-Type"
280+
281+
it 'should have proper Content-Type value', () ->
282+
agentString = "application/json"
283+
assert.equal agentString, subject['Content-Type']
284+
285+
describe "body", () ->
286+
subject = ""
287+
288+
before () ->
289+
subject = output['body']
290+
291+
it 'should contain proper body string', () ->
292+
expectedBody = '{ "message": "hello world" }'
293+
assert.equal expectedBody, subject

0 commit comments

Comments
 (0)