Skip to content

Commit 682cfd0

Browse files
committed
Replace existing headers in WebRequest.Prepare
Fixes #80
1 parent 98d1e56 commit 682cfd0

File tree

4 files changed

+83
-6
lines changed

4 files changed

+83
-6
lines changed

specs/Specs_WebHelpers.bas

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,29 @@ Public Function Specs() As SpecSuite
371371
.Expect(WebHelpers.FindInKeyValues(KeyValues, "d")).ToBeEmpty
372372
End With
373373

374+
' AddOrReplaceInKeyValues
375+
' --------------------------------------------- '
376+
With Specs.It("should add or replace (with retained order) in Key-Values")
377+
Set KeyValues = New Collection
378+
KeyValues.Add WebHelpers.CreateKeyValue("a", 123)
379+
KeyValues.Add WebHelpers.CreateKeyValue("b", 456)
380+
KeyValues.Add WebHelpers.CreateKeyValue("c", 789)
381+
382+
WebHelpers.AddOrReplaceInKeyValues KeyValues, "a", "abc"
383+
WebHelpers.AddOrReplaceInKeyValues KeyValues, "b", "def"
384+
WebHelpers.AddOrReplaceInKeyValues KeyValues, "c", "ghi"
385+
WebHelpers.AddOrReplaceInKeyValues KeyValues, "d", "jkl"
386+
387+
.Expect(KeyValues.Count).ToEqual 4
388+
.Expect(KeyValues(1)("Key")).ToEqual "a"
389+
.Expect(KeyValues(2)("Key")).ToEqual "b"
390+
.Expect(KeyValues(3)("Key")).ToEqual "c"
391+
.Expect(KeyValues(4)("Key")).ToEqual "d"
392+
393+
.Expect(KeyValues(2)("Value")).ToEqual "def"
394+
.Expect(KeyValues(4)("Value")).ToEqual "jkl"
395+
End With
396+
374397
' ============================================= '
375398
' 5. Request preparation / handling
376399
' ============================================= '

specs/Specs_WebRequest.bas

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Public Function Specs() As SpecSuite
1616
Dim Request As WebRequest
1717
Dim Body As Object
1818
Dim Cloned As WebRequest
19+
Dim NumHeaders As Long
1920

2021
WebHelpers.RegisterConverter "csv", "text/csv", "Specs_WebHelpers.SimpleConverter", "Specs_WebHelpers.SimpleParser"
2122

@@ -474,7 +475,7 @@ Public Function Specs() As SpecSuite
474475
Request.ContentLength = 100
475476

476477
.Expect(Request.Headers.Count).ToEqual 0
477-
478+
478479
Request.Prepare
479480

480481
.Expect(Request.Headers.Count).ToBeGTE 3
@@ -483,6 +484,22 @@ Public Function Specs() As SpecSuite
483484
.Expect(WebHelpers.FindInKeyValues(Request.Headers, "Content-Length")).ToEqual "100"
484485
End With
485486

487+
With Specs.It("Prepare should only add headers once")
488+
Set Request = New WebRequest
489+
490+
Request.ContentType = "text/plain"
491+
Request.Accept = "text/csv"
492+
Request.ContentLength = 100
493+
494+
Request.Prepare
495+
496+
NumHeaders = Request.Headers.Count
497+
498+
Request.Prepare
499+
500+
.Expect(Request.Headers.Count).ToEqual NumHeaders
501+
End With
502+
486503
' ============================================= '
487504
' Errors
488505
' ============================================= '

src/WebHelpers.bas

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,43 @@ Public Function FindInKeyValues(KeyValues As Collection, Key As Variant) As Vari
960960
Next web_KeyValue
961961
End Function
962962

963+
''
964+
' Helper for adding/replacing key-value in Collection of key-value
965+
' - Add if key not found
966+
' - Replace if key is found
967+
'
968+
' @param {Collection} KeyValues
969+
' @param {String} Key to find
970+
' @return {Variant}
971+
' --------------------------------------------- '
972+
Public Sub AddOrReplaceInKeyValues(KeyValues As Collection, Key As Variant, Value As Variant)
973+
Dim web_KeyValue As Dictionary
974+
Dim web_Index As Long
975+
Dim web_NewKeyValue As Dictionary
976+
977+
Set web_NewKeyValue = CreateKeyValue(CStr(Key), Value)
978+
979+
web_Index = 1
980+
For Each web_KeyValue In KeyValues
981+
If web_KeyValue("Key") = Key Then
982+
' Replace existing
983+
KeyValues.Remove web_Index
984+
985+
If web_Index > KeyValues.Count Then
986+
KeyValues.Add web_NewKeyValue, After:=web_Index - 1
987+
Else
988+
KeyValues.Add web_NewKeyValue, Before:=web_Index
989+
End If
990+
Exit Sub
991+
End If
992+
993+
web_Index = web_Index + 1
994+
Next web_KeyValue
995+
996+
' Add
997+
KeyValues.Add web_NewKeyValue
998+
End Sub
999+
9631000
' ============================================= '
9641001
' 5. Request preparation / handling
9651002
' ============================================= '

src/WebRequest.cls

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,11 @@ End Sub
248248
' Prepare request for execution
249249
' --------------------------------------------- '
250250
Public Sub Prepare()
251-
' Add general headers to request
252-
Me.AddHeader "User-Agent", WebUserAgent
253-
Me.AddHeader "Content-Type", Me.ContentType
254-
Me.AddHeader "Accept", Me.Accept
255-
Me.AddHeader "Content-Length", VBA.CStr(Me.ContentLength)
251+
' Add/replace general headers for request
252+
WebHelpers.AddOrReplaceInKeyValues Me.Headers, "User-Agent", WebUserAgent
253+
WebHelpers.AddOrReplaceInKeyValues Me.Headers, "Content-Type", Me.ContentType
254+
WebHelpers.AddOrReplaceInKeyValues Me.Headers, "Accept", Me.Accept
255+
WebHelpers.AddOrReplaceInKeyValues Me.Headers, "Content-Length", VBA.CStr(Me.ContentLength)
256256
End Sub
257257

258258
''

0 commit comments

Comments
 (0)