Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for internal/hook/request.go #604

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
164 changes: 164 additions & 0 deletions internal/hook/request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package hook

import (
"encoding/json"
"reflect"
"testing"
)

var parseJSONPayloadTests = []struct {
body []byte
payload map[string]interface{}
ok bool
}{
{[]byte(`[1,2,3]`), map[string]interface{}{"root": []interface{}{json.Number("1"), json.Number("2"), json.Number("3")}}, true},
{[]byte(` [1,2,3]`), map[string]interface{}{"root": []interface{}{json.Number("1"), json.Number("2"), json.Number("3")}}, true},
{[]byte(`{"key": "value"}`), map[string]interface{}{"key": "value"}, true},
{[]byte(`[1, {"]`), map[string]interface{}(nil), false},
{[]byte(`{"key": "value}`), map[string]interface{}(nil), false},
}

func TestParseJSONPayload(t *testing.T) {
for _, tt := range parseJSONPayloadTests {
r := Request{
Body: tt.body,
}
err := r.ParseJSONPayload()
if (err == nil) != tt.ok {
t.Errorf("unexpected result given %q: %s\n", string(tt.body), err)
}

if !reflect.DeepEqual(tt.payload, r.Payload) {
t.Errorf("failed to parse json %q:\nexpected %#v,\ngot %#v", string(tt.body), tt.payload, r.Payload)
}
}
}

var parseHeadersTests = []struct {
headers map[string][]string
expectedHeaders map[string]interface{}
}{
{
map[string][]string{"header1": {"12"}},
map[string]interface{}{"header1": "12"},
},
{
map[string][]string{"header1": {"12", "34"}},
map[string]interface{}{"header1": "12"},
},
{
map[string][]string{"header1": {}},
map[string]interface{}{},
},
}

func TestParseHeaders(t *testing.T) {
for _, tt := range parseHeadersTests {
r := Request{}
r.ParseHeaders(tt.headers)

if !reflect.DeepEqual(tt.expectedHeaders, r.Headers) {
t.Errorf("failed to parse headers %#v:\nexpected %#v,\ngot %#v", tt.headers, tt.expectedHeaders, r.Headers)
}
}
}

var parseQueryTests = []struct {
query map[string][]string
expectedQuery map[string]interface{}
}{
{
map[string][]string{"query1": {"12"}},
map[string]interface{}{"query1": "12"},
},
{
map[string][]string{"query1": {"12", "34"}},
map[string]interface{}{"query1": "12"},
},
{
map[string][]string{"query1": {}},
map[string]interface{}{},
},
}

func TestParseQuery(t *testing.T) {
for _, tt := range parseQueryTests {
r := Request{}
r.ParseQuery(tt.query)

if !reflect.DeepEqual(tt.expectedQuery, r.Query) {
t.Errorf("failed to parse query %#v:\nexpected %#v,\ngot %#v", tt.query, tt.expectedQuery, r.Query)
}
}
}

var parseFormPayloadTests = []struct {
body []byte
expectedPayload map[string]interface{}
ok bool
}{
{
[]byte("x=1&y=2"),
map[string]interface{}{"x": "1", "y": "2"},
true,
},
{
[]byte("x=1&y=2&y=3"),
map[string]interface{}{"x": "1", "y": "2"},
true,
},
{
[]byte(";"),
map[string]interface{}(nil),
false,
},
}

func TestParseFormPayload(t *testing.T) {
for _, tt := range parseFormPayloadTests {
r := Request{
Body: tt.body,
}
err := r.ParseFormPayload()
if (err == nil) != tt.ok {
t.Errorf("unexpected result given %q: %s\n", string(tt.body), err)
}

if !reflect.DeepEqual(tt.expectedPayload, r.Payload) {
t.Errorf("failed to parse form payload %q:\nexpected %#v,\ngot %#v", string(tt.body), tt.expectedPayload, r.Payload)
}
}
}

var parseXMLPayloadTests = []struct {
body []byte
expectedPayload map[string]interface{}
ok bool
}{
{
[]byte("<x>1</x>"),
map[string]interface{}{"x": "1"},
true,
},
{
[]byte("<x>1<x>"),
map[string]interface{}(nil),
false,
},
}

func TestParseXMLPayload(t *testing.T) {
for _, tt := range parseXMLPayloadTests {
r := Request{
Body: tt.body,
}
err := r.ParseXMLPayload()
if (err == nil) != tt.ok {
t.Errorf("unexpected result given %q: %s\n", string(tt.body), err)
}

if !reflect.DeepEqual(tt.expectedPayload, r.Payload) {
t.Errorf("failed to parse xml %q:\nexpected %#v,\ngot %#v", string(tt.body), tt.expectedPayload, r.Payload)
}
}
}