Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions github/repos_hooks_deliveries.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"context"
"encoding/json"
"fmt"
"strings"
)

// HookDelivery represents the data that is received from GitHub's Webhook Delivery API
Expand Down Expand Up @@ -46,6 +47,16 @@
RawPayload *json.RawMessage `json:"payload,omitempty"`
}

// GetHeader gets the value associated with the given key (ignoring key case).
func (r *HookRequest) GetHeader(key string) string {
for k, v := range r.Headers {
if strings.EqualFold(k, key) {
return v
}
}
return ""

Check warning on line 57 in github/repos_hooks_deliveries.go

View check run for this annotation

Codecov / codecov/patch

github/repos_hooks_deliveries.go#L57

Added line #L57 was not covered by tests
}

func (r HookRequest) String() string {
return Stringify(r)
}
Expand All @@ -57,6 +68,16 @@
RawPayload *json.RawMessage `json:"payload,omitempty"`
}

// GetHeader gets the value associated with the given key (ignoring key case).
func (r *HookResponse) GetHeader(key string) string {
for k, v := range r.Headers {
if strings.EqualFold(k, key) {
return v
}
}
return ""

Check warning on line 78 in github/repos_hooks_deliveries.go

View check run for this annotation

Codecov / codecov/patch

github/repos_hooks_deliveries.go#L78

Added line #L78 was not covered by tests
}

func (r HookResponse) String() string {
return Stringify(r)
}
Expand Down
48 changes: 48 additions & 0 deletions github/repos_hooks_deliveries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,30 @@ func TestHookRequest_Marshal(t *testing.T) {
testJSONMarshal(t, r, want)
}

func TestHookRequest_GetHeader(t *testing.T) {
t.Parallel()

header := make(map[string]string)
header["key1"] = "value1"
header["Key+2"] = "value2"
header["kEy-3"] = "value3"
header["KEY_4"] = "value4"

r := &HookRequest{
Headers: header,
}

testPrefixes := []string{"key", "Key", "kEy", "KEY"}
for hdrKey, hdrValue := range header {
for _, prefix := range testPrefixes {
key := prefix + hdrKey[3:]
if val := r.GetHeader(key); val != hdrValue {
t.Errorf("GetHeader(%q) is not working: %q != %q", key, val, hdrValue)
}
}
}
}

func TestHookResponse_Marshal(t *testing.T) {
t.Parallel()
testJSONMarshal(t, &HookResponse{}, "{}")
Expand All @@ -320,6 +344,30 @@ func TestHookResponse_Marshal(t *testing.T) {
testJSONMarshal(t, r, want)
}

func TestHookResponse_GetHeader(t *testing.T) {
t.Parallel()

header := make(map[string]string)
header["key1"] = "value1"
header["Key+2"] = "value2"
header["kEy-3"] = "value3"
header["KEY_4"] = "value4"

r := &HookResponse{
Headers: header,
}

testPrefixes := []string{"key", "Key", "kEy", "KEY"}
for hdrKey, hdrValue := range header {
for _, prefix := range testPrefixes {
key := prefix + hdrKey[3:]
if val := r.GetHeader(key); val != hdrValue {
t.Errorf("GetHeader(%q) is not working: %q != %q", key, val, hdrValue)
}
}
}
}

func TestHookDelivery_Marshal(t *testing.T) {
t.Parallel()
testJSONMarshal(t, &HookDelivery{}, "{}")
Expand Down
Loading