Skip to content

Commit 5b75aa8

Browse files
authored
Add case-instensitive GetHeader for HookRequest & HookResponse (#3556)
1 parent f99490e commit 5b75aa8

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

github/repos_hooks_deliveries.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"context"
1010
"encoding/json"
1111
"fmt"
12+
"strings"
1213
)
1314

1415
// HookDelivery represents the data that is received from GitHub's Webhook Delivery API
@@ -39,13 +40,28 @@ func (d HookDelivery) String() string {
3940
return Stringify(d)
4041
}
4142

43+
// getHeader common function for GetHeader funcs of HookRequest & HookResponse.
44+
func getHeader(headers map[string]string, key string) string {
45+
for k, v := range headers {
46+
if strings.EqualFold(k, key) {
47+
return v
48+
}
49+
}
50+
return ""
51+
}
52+
4253
// HookRequest is a part of HookDelivery that contains
4354
// the HTTP headers and the JSON payload of the webhook request.
4455
type HookRequest struct {
4556
Headers map[string]string `json:"headers,omitempty"`
4657
RawPayload *json.RawMessage `json:"payload,omitempty"`
4758
}
4859

60+
// GetHeader gets the value associated with the given key (ignoring key case).
61+
func (r *HookRequest) GetHeader(key string) string {
62+
return getHeader(r.Headers, key)
63+
}
64+
4965
func (r HookRequest) String() string {
5066
return Stringify(r)
5167
}
@@ -57,6 +73,11 @@ type HookResponse struct {
5773
RawPayload *json.RawMessage `json:"payload,omitempty"`
5874
}
5975

76+
// GetHeader gets the value associated with the given key (ignoring key case).
77+
func (r *HookResponse) GetHeader(key string) string {
78+
return getHeader(r.Headers, key)
79+
}
80+
6081
func (r HookResponse) String() string {
6182
return Stringify(r)
6283
}

github/repos_hooks_deliveries_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,41 @@ func TestHookRequest_Marshal(t *testing.T) {
294294
testJSONMarshal(t, r, want)
295295
}
296296

297+
func TestHookRequest_GetHeader(t *testing.T) {
298+
t.Parallel()
299+
300+
header := make(map[string]string)
301+
header["key1"] = "value1"
302+
header["Key+2"] = "value2"
303+
header["kEy-3"] = "value3"
304+
header["KEY_4"] = "value4"
305+
306+
r := &HookRequest{
307+
Headers: header,
308+
}
309+
310+
// Checking positive cases
311+
testPrefixes := []string{"key", "Key", "kEy", "KEY"}
312+
for hdrKey, hdrValue := range header {
313+
for _, prefix := range testPrefixes {
314+
key := prefix + hdrKey[3:]
315+
if val := r.GetHeader(key); val != hdrValue {
316+
t.Errorf("GetHeader(%q) is not working: %q != %q", key, val, hdrValue)
317+
}
318+
}
319+
}
320+
321+
// Checking negative case
322+
key := "asd"
323+
if val := r.GetHeader(key); val != "" {
324+
t.Errorf("GetHeader(%q) should return empty value: %q != %q", key, val, "")
325+
}
326+
key = "kay1"
327+
if val := r.GetHeader(key); val != "" {
328+
t.Errorf("GetHeader(%q) should return empty value: %q != %q", key, val, "")
329+
}
330+
}
331+
297332
func TestHookResponse_Marshal(t *testing.T) {
298333
t.Parallel()
299334
testJSONMarshal(t, &HookResponse{}, "{}")
@@ -320,6 +355,41 @@ func TestHookResponse_Marshal(t *testing.T) {
320355
testJSONMarshal(t, r, want)
321356
}
322357

358+
func TestHookResponse_GetHeader(t *testing.T) {
359+
t.Parallel()
360+
361+
header := make(map[string]string)
362+
header["key1"] = "value1"
363+
header["Key+2"] = "value2"
364+
header["kEy-3"] = "value3"
365+
header["KEY_4"] = "value4"
366+
367+
r := &HookResponse{
368+
Headers: header,
369+
}
370+
371+
// Checking positive cases
372+
testPrefixes := []string{"key", "Key", "kEy", "KEY"}
373+
for hdrKey, hdrValue := range header {
374+
for _, prefix := range testPrefixes {
375+
key := prefix + hdrKey[3:]
376+
if val := r.GetHeader(key); val != hdrValue {
377+
t.Errorf("GetHeader(%q) is not working: %q != %q", key, val, hdrValue)
378+
}
379+
}
380+
}
381+
382+
// Checking negative case
383+
key := "asd"
384+
if val := r.GetHeader(key); val != "" {
385+
t.Errorf("GetHeader(%q) should return empty value: %q != %q", key, val, "")
386+
}
387+
key = "kay1"
388+
if val := r.GetHeader(key); val != "" {
389+
t.Errorf("GetHeader(%q) should return empty value: %q != %q", key, val, "")
390+
}
391+
}
392+
323393
func TestHookDelivery_Marshal(t *testing.T) {
324394
t.Parallel()
325395
testJSONMarshal(t, &HookDelivery{}, "{}")

0 commit comments

Comments
 (0)