Skip to content

Commit

Permalink
Merge pull request #25 from timhall/cookies
Browse files Browse the repository at this point in the history
Add cookie support
  • Loading branch information
timhall committed Feb 19, 2014
2 parents 1fecddc + f301bec commit 5b7b7fb
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 55 deletions.
3 changes: 3 additions & 0 deletions build/export-specs.vbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Set Args = Wscript.Arguments
If Args.Length > 0 Then
WBPath = Args(0)
OutputPath = Args(1)
Else
WBPath = "specs\Excel-REST - Specs.xlsm"
OutputPath = "specs\"
End If

' Setup modules to export
Expand Down
Binary file modified specs/Excel-REST - Specs.xlsm
Binary file not shown.
65 changes: 65 additions & 0 deletions specs/RestClientAsyncSpecs.bas
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Public Function Specs() As SpecSuite
SimpleCallback = "RestClientAsyncSpecs.SimpleCallback"
ComplexCallback = "RestClientAsyncSpecs.ComplexCallback"

Dim BodyToString As String

With Specs.It("should pass response to callback")
Set Request = New RestRequest
Request.Resource = "get"
Expand Down Expand Up @@ -86,6 +88,68 @@ Public Function Specs() As SpecSuite
.Expect(AsyncResponse.StatusDescription).ToEqual "Internal Server Error"
End With

With Specs.It("should include binary body in response")
Set Request = New RestRequest
Request.Resource = "howdy"

Client.ExecuteAsync Request, SimpleCallback
Wait WaitTime
.Expect(AsyncResponse).ToBeDefined
If Not AsyncResponse Is Nothing Then
.Expect(AsyncResponse.Body).ToBeDefined

If Not IsEmpty(AsyncResponse.Body) Then
For i = LBound(AsyncResponse.Body) To UBound(AsyncResponse.Body)
BodyToString = BodyToString & Chr(AsyncResponse.Body(i))
Next i
End If

.Expect(BodyToString).ToEqual "Howdy!"
End If
End With

With Specs.It("should include cookies in response")
Set Request = New RestRequest
Request.Resource = "cookie"

Client.ExecuteAsync Request, SimpleCallback
Wait WaitTime
.Expect(AsyncResponse).ToBeDefined
If Not AsyncResponse Is Nothing Then
.Expect(AsyncResponse.Cookies.count).ToEqual 4
.Expect(AsyncResponse.Cookies("unsigned-cookie")).ToEqual "simple-cookie"
.Expect(AsyncResponse.Cookies("signed-cookie")).ToContain "special-cookie"
.Expect(AsyncResponse.Cookies("tricky;cookie")).ToEqual "includes; semi-colon and space at end "
.Expect(AsyncResponse.Cookies("duplicate-cookie")).ToEqual "B"
End If
End With

With Specs.It("should include cookies with request")
Set Request = New RestRequest
Request.Resource = "cookie"

Set Response = Client.Execute(Request)

Set Request = New RestRequest
Request.Resource = "get"
Request.AddCookie "test-cookie", "howdy"
Request.AddCookie "signed-cookie", Response.Cookies("signed-cookie")

Client.ExecuteAsync Request, SimpleCallback
Wait WaitTime
.Expect(AsyncResponse).ToBeDefined
If Not AsyncResponse Is Nothing Then
.Expect(AsyncResponse.Data).ToBeDefined
If Not IsEmpty(AsyncResponse.Data) Then
.Expect(AsyncResponse.Data("cookies").count).ToEqual 1
.Expect(AsyncResponse.Data("cookies")("test-cookie")).ToEqual "howdy"
.Expect(AsyncResponse.Data("signed_cookies").count).ToEqual 1
.Expect(AsyncResponse.Data("signed_cookies")("signed-cookie")).ToEqual "special-cookie"
End If
End If
End With

' Note: Weird async issues can occur if timeout spec isn't last
With Specs.It("should return 408 and close request on request timeout")
Set Request = New RestRequest
Request.Resource = "timeout"
Expand All @@ -100,6 +164,7 @@ Public Function Specs() As SpecSuite
.Expect(AsyncResponse.StatusDescription).ToEqual "Request Timeout"
End If
.Expect(Request.HttpRequest).ToBeUndefined
Client.TimeoutMS = 2000
End With

InlineRunner.RunSuite Specs
Expand Down
50 changes: 49 additions & 1 deletion specs/RestClientSpecs.bas
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Public Function Specs() As SpecSuite
Dim Request As RestRequest
Dim Response As RestResponse
Dim Body As Dictionary
Dim BodyToString As String
Dim i As Integer

Client.BaseUrl = "localhost:3000/"

Expand Down Expand Up @@ -125,7 +127,7 @@ Public Function Specs() As SpecSuite
Set Response = Client.Execute(Request)
.Expect(Response.StatusCode).ToEqual 408
.Expect(Response.StatusDescription).ToEqual "Request Timeout"
Debug.Print Response.Content
Client.TimeoutMS = 2000
End With

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

With Specs.It("should include binary body in response")
Set Request = New RestRequest
Request.Resource = "howdy"

Set Response = Client.Execute(Request)
.Expect(Response.Body).ToBeDefined

If Not IsEmpty(Response.Body) Then
For i = LBound(Response.Body) To UBound(Response.Body)
BodyToString = BodyToString & Chr(Response.Body(i))
Next i
End If

.Expect(BodyToString).ToEqual "Howdy!"
End With

With Specs.It("should include cookies in response")
Set Request = New RestRequest
Request.Resource = "cookie"

Set Response = Client.Execute(Request)
.Expect(Response.Cookies.count).ToEqual 4
.Expect(Response.Cookies("unsigned-cookie")).ToEqual "simple-cookie"
.Expect(Response.Cookies("signed-cookie")).ToContain "special-cookie"
.Expect(Response.Cookies("tricky;cookie")).ToEqual "includes; semi-colon and space at end "
.Expect(Response.Cookies("duplicate-cookie")).ToEqual "B"
End With

With Specs.It("should include cookies with request")
Set Request = New RestRequest
Request.Resource = "cookie"

Set Response = Client.Execute(Request)

Set Request = New RestRequest
Request.Resource = "get"
Request.AddCookie "test-cookie", "howdy"
Request.AddCookie "signed-cookie", Response.Cookies("signed-cookie")

Set Response = Client.Execute(Request)
.Expect(Response.Data("cookies").count).ToEqual 1
.Expect(Response.Data("cookies")("test-cookie")).ToEqual "howdy"
.Expect(Response.Data("signed_cookies").count).ToEqual 1
.Expect(Response.Data("signed_cookies")("signed-cookie")).ToEqual "special-cookie"
End With

Set Client = Nothing

InlineRunner.RunSuite Specs
Expand Down
31 changes: 31 additions & 0 deletions specs/RestHelpersSpecs.bas
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Public Function Specs() As SpecSuite
Dim Combined As Object
Dim Whitelist As Variant
Dim Filtered As Object
Dim ResponseHeaders As String
Dim Headers As Collection
Dim Cookies As Dictionary

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

With Specs.It("should decode url values")
.Expect(RestHelpers.URLDecode("+%20%21%22%23%24%25%26%27")).ToEqual " !""#$%&'"
End With

With Specs.It("should join url with /")
.Expect(RestHelpers.JoinUrl("a", "b")).ToEqual "a/b"
.Expect(RestHelpers.JoinUrl("a/", "b")).ToEqual "a/b"
Expand Down Expand Up @@ -148,6 +155,30 @@ Public Function Specs() As SpecSuite
.Expect(Filtered.Exists("dangerous")).ToEqual False
End With

With Specs.It("should extract headers from response headers")
ResponseHeaders = "Connection: keep -alive" & vbCrLf & _
"Date: Tue, 18 Feb 2014 15:00:26 GMT" & vbCrLf & _
"Content-Length: 2" & vbCrLf & _
"Content-Type: text/plain" & vbCrLf & _
"Set-Cookie: unsigned-cookie=simple-cookie; Path=/" & vbCrLf & _
"Set-Cookie: signed-cookie=s%3Aspecial-cookie.1Ghgw2qpDY93QdYjGFPDLAsa3%2FI0FCtO%2FvlxoHkzF%2BY; Path=/" & vbCrLf & _
"Set-Cookie: duplicate-cookie=A; Path=/" & vbCrLf & _
"Set-Cookie: duplicate-cookie=B" & vbCrLf & _
"X-Powered-By: Express"

Set Headers = RestHelpers.ExtractHeadersFromResponseHeaders(ResponseHeaders)
.Expect(Headers.count).ToEqual 9
.Expect(Headers.Item(5)("key")).ToEqual "Set-Cookie"
.Expect(Headers.Item(5)("value")).ToEqual "unsigned-cookie=simple-cookie; Path=/"
End With

With Specs.It("should extract cookies from response headers")
Set Cookies = RestHelpers.ExtractCookiesFromResponseHeaders(ResponseHeaders)
.Expect(Cookies.count).ToEqual 3
.Expect(Cookies("unsigned-cookie")).ToEqual "simple-cookie"
.Expect(Cookies("duplicate-cookie")).ToEqual "B"
End With

With Specs.It("should encode string to base64")
.Expect(RestHelpers.EncodeStringToBase64("Howdy!")).ToEqual "SG93ZHkh"
End With
Expand Down
20 changes: 19 additions & 1 deletion specs/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ app.use(function(req, res, next){
app.use(plain());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.cookieParser('cookie-secret'));

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

// Cookies
app.get('/cookie', function(req, res) {
res.cookie('unsigned-cookie', 'simple-cookie');
res.cookie('signed-cookie', 'special-cookie', {signed: true});
res.cookie('tricky;cookie', 'includes; semi-colon and space at end ');
res.cookie('duplicate-cookie', 'A');
res.cookie('duplicate-cookie', 'B');
res.send(200);
});

// Simple text in body
app.get('/howdy', function(req, res) {
res.send(200, 'Howdy!');
});

function standardResponse(req, res) {
res.send(200, {
method: req.route.method.toUpperCase(),
Expand All @@ -44,7 +60,9 @@ function standardResponse(req, res) {
'content-type': req.get('content-type'),
'custom': req.get('custom')
},
body: req.text || req.body
body: req.text || req.body,
cookies: req.cookies,
signed_cookies: req.signedCookies
});
}

Expand Down
Loading

0 comments on commit 5b7b7fb

Please sign in to comment.