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

feat: invocation request with url encoded form #8

Merged
merged 1 commit into from
Oct 30, 2024
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
31 changes: 31 additions & 0 deletions invocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"io"
"net/http"
"net/url"

assertions "github.com/stretchr/testify/assert"
)
Expand All @@ -15,6 +16,11 @@ type Invocation struct {
testState TestingT
}

type InvocationRequestForm struct {
invocation *Invocation
formValues url.Values
}

func newInvocation(request *http.Request, testState TestingT) *Invocation {
var data []byte
var err error
Expand Down Expand Up @@ -71,3 +77,28 @@ func (call *Invocation) ReadJSONPayload(obj any) {
call.testState.Fatal(err)
}
}

func (call *Invocation) WithUrlEncodedFormPayload() *InvocationRequestForm {
formValues, err := url.ParseQuery(string(call.GetPayload()))
if err != nil {
call.testState.Fatal(err)
return nil
}
return &InvocationRequestForm{
invocation: call.WithHeader("Content-Type", "application/x-www-form-urlencoded"),
formValues: formValues,
}
}

func (form InvocationRequestForm) WithValues(expectedValues map[string]string) {
for key, value := range expectedValues {
assertions.Equal(form.invocation.testState, value, form.formValues.Get(key))
}
}

func (form InvocationRequestForm) WithValuesExactly(expectedValues map[string]string) {
assertions.Equal(form.invocation.testState, len(expectedValues), len(form.formValues))
for key, value := range expectedValues {
assertions.Equal(form.invocation.testState, value, form.formValues.Get(key))
}
}
98 changes: 98 additions & 0 deletions invocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,104 @@ func TestInvocation_ReadJsonPayload_ErrorHandling(t *testing.T) {
testState.assertFailedWithFatal()
}

func TestInvocation_WithUrlEncodedForm_Fail(t *testing.T) {
t.Parallel()
request := buildRequestWithBody(t, []byte("key1=value1&key2=value+2%21"))

testState := NewTestingMock(t)
invocation := newInvocation(request, testState)

invocation.WithUrlEncodedFormPayload()
testState.assertFailedWithError()
}

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

testCases := []map[string]string{
{"key1": "value1"},
{"key1": "value1", "key2": "value 2!"},
}

for i := range testCases {
values := testCases[i]
t.Run("", func(t *testing.T) {
t.Parallel()
request := buildRequestWithBody(t, []byte("key1=value1&key2=value+2%21"))
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
testState := NewTestingMock(t)
invocation := newInvocation(request, testState)

invocation.WithUrlEncodedFormPayload().WithValues(values)

testState.assertDidNotFailed()
})
}
}

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

testCases := []map[string]string{
{"key1": "not value1"},
{"not_key1": "value1"},
}

for i := range testCases {
values := testCases[i]
t.Run("", func(t *testing.T) {
t.Parallel()
request := buildRequestWithBody(t, []byte("key1=value1&key2=value+2%21"))
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
testState := NewTestingMock(t)
invocation := newInvocation(request, testState)

invocation.WithUrlEncodedFormPayload().WithValues(values)

testState.assertFailedWithError()
})
}
}

func TestInvocation_WithUrlEncodedForm_ValuesExactly_Pass(t *testing.T) {
t.Parallel()
request := buildRequestWithBody(t, []byte("key1=value1&key2=value+2%21"))
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")

testState := NewTestingMock(t)
invocation := newInvocation(request, testState)
invocation.
WithUrlEncodedFormPayload().
WithValuesExactly(map[string]string{"key1": "value1", "key2": "value 2!"})

testState.assertDidNotFailed()
}

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

testCases := []map[string]string{
{"key1": "value1"},
{"key1": "not value1"},
{"not_key1": "value1"},
}

for i := range testCases {
values := testCases[i]
t.Run("", func(t *testing.T) {
t.Parallel()
request := buildRequestWithBody(t, []byte("key1=value1&key2=value+2%21"))
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
testState := NewTestingMock(t)
invocation := newInvocation(request, testState)

invocation.WithUrlEncodedFormPayload().WithValuesExactly(values)

testState.assertFailedWithError()
})
}
}

func buildRequest(t *testing.T, method string, url string, data []byte) *http.Request {
request, err := http.NewRequest(method, url, bytes.NewReader(data))
if err != nil {
Expand Down