+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
)

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

// getHeader common function for GetHeader funcs of HookRequest & HookResponse.
func getHeader(headers map[string]string, key string) string {
for k, v := range headers {
if strings.EqualFold(k, key) {
return v
}
}
return ""
}

// HookRequest is a part of HookDelivery that contains
// the HTTP headers and the JSON payload of the webhook request.
type HookRequest struct {
Headers map[string]string `json:"headers,omitempty"`
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 {
return getHeader(r.Headers, key)
}

func (r HookRequest) String() string {
return Stringify(r)
}
Expand All @@ -57,6 +73,11 @@ type HookResponse struct {
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 {
return getHeader(r.Headers, key)
}

func (r HookResponse) String() string {
return Stringify(r)
}
Expand Down
70 changes: 70 additions & 0 deletions github/repos_hooks_deliveries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,41 @@ 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,
}

// Checking positive cases
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)
}
}
}

// Checking negative case
key := "asd"
if val := r.GetHeader(key); val != "" {
t.Errorf("GetHeader(%q) should return empty value: %q != %q", key, val, "")
}
key = "kay1"
if val := r.GetHeader(key); val != "" {
t.Errorf("GetHeader(%q) should return empty value: %q != %q", key, val, "")
}
}

func TestHookResponse_Marshal(t *testing.T) {
t.Parallel()
testJSONMarshal(t, &HookResponse{}, "{}")
Expand All @@ -320,6 +355,41 @@ 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,
}

// Checking positive cases
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)
}
}
}

// Checking negative case
key := "asd"
if val := r.GetHeader(key); val != "" {
t.Errorf("GetHeader(%q) should return empty value: %q != %q", key, val, "")
}
key = "kay1"
if val := r.GetHeader(key); val != "" {
t.Errorf("GetHeader(%q) should return empty value: %q != %q", key, val, "")
}
}

func TestHookDelivery_Marshal(t *testing.T) {
t.Parallel()
testJSONMarshal(t, &HookDelivery{}, "{}")
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载