Skip to content

Commit 621d09f

Browse files
committed
Add form-urlencoded specs
1 parent f1c4b9d commit 621d09f

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

specs/Excel-REST - Specs.xlsm

43.7 KB
Binary file not shown.

specs/RestHelpersSpecs.bas

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ Public Function Specs() As SpecSuite
1818
Dim Parsed As Object
1919
Dim Obj As Object
2020
Dim Coll As Collection
21-
Dim A As Object
22-
Dim B As Object
23-
Dim Combined As Object
21+
Dim A As Dictionary
22+
Dim B As Dictionary
23+
Dim Combined As Dictionary
2424
Dim Whitelist As Variant
25-
Dim Filtered As Object
25+
Dim Filtered As Dictionary
26+
Dim Encoded As String
2627
Dim ResponseHeaders As String
2728
Dim Headers As Collection
2829
Dim Cookies As Dictionary
@@ -107,10 +108,14 @@ Public Function Specs() As SpecSuite
107108

108109
With Specs.It("should url encode values")
109110
.Expect(RestHelpers.URLEncode(" !""#$%&'")).ToEqual "%20%21%22%23%24%25%26%27"
111+
.Expect(RestHelpers.URLEncode("A + B")).ToEqual "A%20%2B%20B"
112+
.Expect(RestHelpers.URLEncode("A + B", True)).ToEqual "A+%2B+B"
110113
End With
111114

112115
With Specs.It("should decode url values")
113116
.Expect(RestHelpers.URLDecode("+%20%21%22%23%24%25%26%27")).ToEqual " !""#$%&'"
117+
.Expect(RestHelpers.URLDecode("A%20%2B%20B")).ToEqual "A + B"
118+
.Expect(RestHelpers.URLDecode("A+%2B+B")).ToEqual "A + B"
114119
End With
115120

116121
With Specs.It("should join url with /")
@@ -155,6 +160,29 @@ Public Function Specs() As SpecSuite
155160
.Expect(Filtered.Exists("dangerous")).ToEqual False
156161
End With
157162

163+
With Specs.It("should combine and convert parameters to url-encoded string")
164+
Set A = New Dictionary
165+
Set B = New Dictionary
166+
167+
A.Add "a", 1
168+
A.Add "b", 3.14
169+
B.Add "b", 4.14
170+
B.Add "c", "Howdy!"
171+
B.Add "d & e", "A + B"
172+
173+
Encoded = RestHelpers.DictionariesToUrlEncodedString(A, B)
174+
.Expect(Encoded).ToEqual "a=1&b=4.14&c=Howdy%21&d+%26+e=A+%2B+B"
175+
End With
176+
177+
With Specs.It("should parse url-encoded string")
178+
Set Parsed = RestHelpers.ParseUrlEncoded("a=1&b=3.14&c=Howdy%21&d+%26+e=A+%2B+B")
179+
180+
.Expect(Parsed("a")).ToEqual "1"
181+
.Expect(Parsed("b")).ToEqual "3.14"
182+
.Expect(Parsed("c")).ToEqual "Howdy!"
183+
.Expect(Parsed("d & e")).ToEqual "A + B"
184+
End With
185+
158186
With Specs.It("should extract headers from response headers")
159187
ResponseHeaders = "Connection: keep -alive" & vbCrLf & _
160188
"Date: Tue, 18 Feb 2014 15:00:26 GMT" & vbCrLf & _

specs/RestRequestSpecs.bas

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Public Function Specs() As SpecSuite
9797
Request.AddParameter "A B", " !""#$%&'"
9898
Request.Method = httpGET
9999

100-
.Expect(Request.FormattedResource).ToEqual "?A%20B=%20%21%22%23%24%25%26%27"
100+
.Expect(Request.FormattedResource).ToEqual "?A+B=+%21%22%23%24%25%26%27"
101101
End With
102102

103103
With Specs.It("should include cachebreaker in FormattedResource by default")
@@ -144,7 +144,7 @@ Public Function Specs() As SpecSuite
144144
.Expect(Request.Body).ToEqual "{""A"":123}"
145145

146146
Request.Method = httpPOST
147-
.Expect(Request.Body).ToEqual "{""A"":123,""b"":456}"
147+
.Expect(Request.Body).ToEqual "{""b"":456,""A"":123}"
148148
End With
149149

150150
With Specs.It("should use given client base url for FullUrl only if BaseUrl isn't already set")
@@ -175,25 +175,20 @@ Public Function Specs() As SpecSuite
175175
.Expect(Request.FullUrl("facebook.com/api/")).ToEqual "https://facebook.com/api/status"
176176
End With
177177

178-
With Specs.It("should user form-urlencoded content type for non-GET requests with parameters")
178+
With Specs.It("should include content-type based on specified format")
179179
Set Request = New RestRequest
180180

181181
Request.AddParameter "A", 123
182182
Request.Method = httpPOST
183183

184-
.Expect(Request.ContentType).ToEqual "application/x-www-form-urlencoded;charset=UTF-8"
185-
End With
186-
187-
With Specs.It("should use application/json for GET requests with parameters and requests without parameters")
188-
Set Request = New RestRequest
189-
190-
Request.Method = httpPOST
184+
' JSON by default
191185
.Expect(Request.ContentType).ToEqual "application/json"
192186

193-
Request.AddParameter "A", 123
194-
Request.Method = httpGET
195-
187+
Request.Format = json
196188
.Expect(Request.ContentType).ToEqual "application/json"
189+
190+
Request.Format = formurlencoded
191+
.Expect(Request.ContentType).ToEqual "application/x-www-form-urlencoded;charset=UTF-8"
197192
End With
198193

199194
With Specs.It("should override existing headers, url segments, and parameters")
@@ -260,6 +255,23 @@ Public Function Specs() As SpecSuite
260255
.Expect(Request.Body).ToEqual "Howdy!"
261256
End With
262257

258+
With Specs.It("should format body based on set format")
259+
Set Request = New RestRequest
260+
Request.Method = httpPOST
261+
262+
Request.AddParameter "A", 123
263+
Request.AddParameter "B", "Howdy!"
264+
265+
' JSON by default
266+
.Expect(Request.Body).ToEqual "{""A"":123,""B"":""Howdy!""}"
267+
268+
Request.Format = json
269+
.Expect(Request.Body).ToEqual "{""A"":123,""B"":""Howdy!""}"
270+
271+
Request.Format = formurlencoded
272+
.Expect(Request.Body).ToEqual "A=123&B=Howdy%21"
273+
End With
274+
263275
InlineRunner.RunSuite Specs
264276
End Function
265277

0 commit comments

Comments
 (0)