Skip to content

Commit 5b7b7fb

Browse files
committed
Merge pull request #25 from timhall/cookies
Add cookie support
2 parents 1fecddc + f301bec commit 5b7b7fb

File tree

9 files changed

+359
-55
lines changed

9 files changed

+359
-55
lines changed

build/export-specs.vbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Set Args = Wscript.Arguments
1818
If Args.Length > 0 Then
1919
WBPath = Args(0)
2020
OutputPath = Args(1)
21+
Else
22+
WBPath = "specs\Excel-REST - Specs.xlsm"
23+
OutputPath = "specs\"
2124
End If
2225

2326
' Setup modules to export

specs/Excel-REST - Specs.xlsm

53 KB
Binary file not shown.

specs/RestClientAsyncSpecs.bas

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Public Function Specs() As SpecSuite
3232
SimpleCallback = "RestClientAsyncSpecs.SimpleCallback"
3333
ComplexCallback = "RestClientAsyncSpecs.ComplexCallback"
3434

35+
Dim BodyToString As String
36+
3537
With Specs.It("should pass response to callback")
3638
Set Request = New RestRequest
3739
Request.Resource = "get"
@@ -86,6 +88,68 @@ Public Function Specs() As SpecSuite
8688
.Expect(AsyncResponse.StatusDescription).ToEqual "Internal Server Error"
8789
End With
8890

91+
With Specs.It("should include binary body in response")
92+
Set Request = New RestRequest
93+
Request.Resource = "howdy"
94+
95+
Client.ExecuteAsync Request, SimpleCallback
96+
Wait WaitTime
97+
.Expect(AsyncResponse).ToBeDefined
98+
If Not AsyncResponse Is Nothing Then
99+
.Expect(AsyncResponse.Body).ToBeDefined
100+
101+
If Not IsEmpty(AsyncResponse.Body) Then
102+
For i = LBound(AsyncResponse.Body) To UBound(AsyncResponse.Body)
103+
BodyToString = BodyToString & Chr(AsyncResponse.Body(i))
104+
Next i
105+
End If
106+
107+
.Expect(BodyToString).ToEqual "Howdy!"
108+
End If
109+
End With
110+
111+
With Specs.It("should include cookies in response")
112+
Set Request = New RestRequest
113+
Request.Resource = "cookie"
114+
115+
Client.ExecuteAsync Request, SimpleCallback
116+
Wait WaitTime
117+
.Expect(AsyncResponse).ToBeDefined
118+
If Not AsyncResponse Is Nothing Then
119+
.Expect(AsyncResponse.Cookies.count).ToEqual 4
120+
.Expect(AsyncResponse.Cookies("unsigned-cookie")).ToEqual "simple-cookie"
121+
.Expect(AsyncResponse.Cookies("signed-cookie")).ToContain "special-cookie"
122+
.Expect(AsyncResponse.Cookies("tricky;cookie")).ToEqual "includes; semi-colon and space at end "
123+
.Expect(AsyncResponse.Cookies("duplicate-cookie")).ToEqual "B"
124+
End If
125+
End With
126+
127+
With Specs.It("should include cookies with request")
128+
Set Request = New RestRequest
129+
Request.Resource = "cookie"
130+
131+
Set Response = Client.Execute(Request)
132+
133+
Set Request = New RestRequest
134+
Request.Resource = "get"
135+
Request.AddCookie "test-cookie", "howdy"
136+
Request.AddCookie "signed-cookie", Response.Cookies("signed-cookie")
137+
138+
Client.ExecuteAsync Request, SimpleCallback
139+
Wait WaitTime
140+
.Expect(AsyncResponse).ToBeDefined
141+
If Not AsyncResponse Is Nothing Then
142+
.Expect(AsyncResponse.Data).ToBeDefined
143+
If Not IsEmpty(AsyncResponse.Data) Then
144+
.Expect(AsyncResponse.Data("cookies").count).ToEqual 1
145+
.Expect(AsyncResponse.Data("cookies")("test-cookie")).ToEqual "howdy"
146+
.Expect(AsyncResponse.Data("signed_cookies").count).ToEqual 1
147+
.Expect(AsyncResponse.Data("signed_cookies")("signed-cookie")).ToEqual "special-cookie"
148+
End If
149+
End If
150+
End With
151+
152+
' Note: Weird async issues can occur if timeout spec isn't last
89153
With Specs.It("should return 408 and close request on request timeout")
90154
Set Request = New RestRequest
91155
Request.Resource = "timeout"
@@ -100,6 +164,7 @@ Public Function Specs() As SpecSuite
100164
.Expect(AsyncResponse.StatusDescription).ToEqual "Request Timeout"
101165
End If
102166
.Expect(Request.HttpRequest).ToBeUndefined
167+
Client.TimeoutMS = 2000
103168
End With
104169

105170
InlineRunner.RunSuite Specs

specs/RestClientSpecs.bas

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Public Function Specs() As SpecSuite
1818
Dim Request As RestRequest
1919
Dim Response As RestResponse
2020
Dim Body As Dictionary
21+
Dim BodyToString As String
22+
Dim i As Integer
2123

2224
Client.BaseUrl = "localhost:3000/"
2325

@@ -125,7 +127,7 @@ Public Function Specs() As SpecSuite
125127
Set Response = Client.Execute(Request)
126128
.Expect(Response.StatusCode).ToEqual 408
127129
.Expect(Response.StatusDescription).ToEqual "Request Timeout"
128-
Debug.Print Response.Content
130+
Client.TimeoutMS = 2000
129131
End With
130132

131133
With Specs.It("should add content-length header (if enabled)")
@@ -158,6 +160,52 @@ Public Function Specs() As SpecSuite
158160
.Expect(Request.Headers.Exists("Content-Length")).ToEqual False
159161
End With
160162

163+
With Specs.It("should include binary body in response")
164+
Set Request = New RestRequest
165+
Request.Resource = "howdy"
166+
167+
Set Response = Client.Execute(Request)
168+
.Expect(Response.Body).ToBeDefined
169+
170+
If Not IsEmpty(Response.Body) Then
171+
For i = LBound(Response.Body) To UBound(Response.Body)
172+
BodyToString = BodyToString & Chr(Response.Body(i))
173+
Next i
174+
End If
175+
176+
.Expect(BodyToString).ToEqual "Howdy!"
177+
End With
178+
179+
With Specs.It("should include cookies in response")
180+
Set Request = New RestRequest
181+
Request.Resource = "cookie"
182+
183+
Set Response = Client.Execute(Request)
184+
.Expect(Response.Cookies.count).ToEqual 4
185+
.Expect(Response.Cookies("unsigned-cookie")).ToEqual "simple-cookie"
186+
.Expect(Response.Cookies("signed-cookie")).ToContain "special-cookie"
187+
.Expect(Response.Cookies("tricky;cookie")).ToEqual "includes; semi-colon and space at end "
188+
.Expect(Response.Cookies("duplicate-cookie")).ToEqual "B"
189+
End With
190+
191+
With Specs.It("should include cookies with request")
192+
Set Request = New RestRequest
193+
Request.Resource = "cookie"
194+
195+
Set Response = Client.Execute(Request)
196+
197+
Set Request = New RestRequest
198+
Request.Resource = "get"
199+
Request.AddCookie "test-cookie", "howdy"
200+
Request.AddCookie "signed-cookie", Response.Cookies("signed-cookie")
201+
202+
Set Response = Client.Execute(Request)
203+
.Expect(Response.Data("cookies").count).ToEqual 1
204+
.Expect(Response.Data("cookies")("test-cookie")).ToEqual "howdy"
205+
.Expect(Response.Data("signed_cookies").count).ToEqual 1
206+
.Expect(Response.Data("signed_cookies")("signed-cookie")).ToEqual "special-cookie"
207+
End With
208+
161209
Set Client = Nothing
162210

163211
InlineRunner.RunSuite Specs

specs/RestHelpersSpecs.bas

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Public Function Specs() As SpecSuite
2323
Dim Combined As Object
2424
Dim Whitelist As Variant
2525
Dim Filtered As Object
26+
Dim ResponseHeaders As String
27+
Dim Headers As Collection
28+
Dim Cookies As Dictionary
2629

2730
With Specs.It("should parse json")
2831
json = "{""a"":1,""b"":3.14,""c"":""Howdy!"",""d"":true,""e"":[1,2]}"
@@ -106,6 +109,10 @@ Public Function Specs() As SpecSuite
106109
.Expect(RestHelpers.URLEncode(" !""#$%&'")).ToEqual "%20%21%22%23%24%25%26%27"
107110
End With
108111

112+
With Specs.It("should decode url values")
113+
.Expect(RestHelpers.URLDecode("+%20%21%22%23%24%25%26%27")).ToEqual " !""#$%&'"
114+
End With
115+
109116
With Specs.It("should join url with /")
110117
.Expect(RestHelpers.JoinUrl("a", "b")).ToEqual "a/b"
111118
.Expect(RestHelpers.JoinUrl("a/", "b")).ToEqual "a/b"
@@ -148,6 +155,30 @@ Public Function Specs() As SpecSuite
148155
.Expect(Filtered.Exists("dangerous")).ToEqual False
149156
End With
150157

158+
With Specs.It("should extract headers from response headers")
159+
ResponseHeaders = "Connection: keep -alive" & vbCrLf & _
160+
"Date: Tue, 18 Feb 2014 15:00:26 GMT" & vbCrLf & _
161+
"Content-Length: 2" & vbCrLf & _
162+
"Content-Type: text/plain" & vbCrLf & _
163+
"Set-Cookie: unsigned-cookie=simple-cookie; Path=/" & vbCrLf & _
164+
"Set-Cookie: signed-cookie=s%3Aspecial-cookie.1Ghgw2qpDY93QdYjGFPDLAsa3%2FI0FCtO%2FvlxoHkzF%2BY; Path=/" & vbCrLf & _
165+
"Set-Cookie: duplicate-cookie=A; Path=/" & vbCrLf & _
166+
"Set-Cookie: duplicate-cookie=B" & vbCrLf & _
167+
"X-Powered-By: Express"
168+
169+
Set Headers = RestHelpers.ExtractHeadersFromResponseHeaders(ResponseHeaders)
170+
.Expect(Headers.count).ToEqual 9
171+
.Expect(Headers.Item(5)("key")).ToEqual "Set-Cookie"
172+
.Expect(Headers.Item(5)("value")).ToEqual "unsigned-cookie=simple-cookie; Path=/"
173+
End With
174+
175+
With Specs.It("should extract cookies from response headers")
176+
Set Cookies = RestHelpers.ExtractCookiesFromResponseHeaders(ResponseHeaders)
177+
.Expect(Cookies.count).ToEqual 3
178+
.Expect(Cookies("unsigned-cookie")).ToEqual "simple-cookie"
179+
.Expect(Cookies("duplicate-cookie")).ToEqual "B"
180+
End With
181+
151182
With Specs.It("should encode string to base64")
152183
.Expect(RestHelpers.EncodeStringToBase64("Howdy!")).ToEqual "SG93ZHkh"
153184
End With

specs/server.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ app.use(function(req, res, next){
99
app.use(plain());
1010
app.use(express.json());
1111
app.use(express.urlencoded());
12+
app.use(express.cookieParser('cookie-secret'));
1213

1314
// Standard
1415
app.get('/get', standardResponse);
@@ -36,6 +37,21 @@ app.get('/json', function(req, res) {
3637
res.json({a: '1', b: 2, c: 3.14, d: false, e: [4, 5], f: {a: '1', b: 2}});
3738
});
3839

40+
// Cookies
41+
app.get('/cookie', function(req, res) {
42+
res.cookie('unsigned-cookie', 'simple-cookie');
43+
res.cookie('signed-cookie', 'special-cookie', {signed: true});
44+
res.cookie('tricky;cookie', 'includes; semi-colon and space at end ');
45+
res.cookie('duplicate-cookie', 'A');
46+
res.cookie('duplicate-cookie', 'B');
47+
res.send(200);
48+
});
49+
50+
// Simple text in body
51+
app.get('/howdy', function(req, res) {
52+
res.send(200, 'Howdy!');
53+
});
54+
3955
function standardResponse(req, res) {
4056
res.send(200, {
4157
method: req.route.method.toUpperCase(),
@@ -44,7 +60,9 @@ function standardResponse(req, res) {
4460
'content-type': req.get('content-type'),
4561
'custom': req.get('custom')
4662
},
47-
body: req.text || req.body
63+
body: req.text || req.body,
64+
cookies: req.cookies,
65+
signed_cookies: req.signedCookies
4866
});
4967
}
5068

0 commit comments

Comments
 (0)