Skip to content

Commit

Permalink
feat: invocation request with url encoded form (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
le-yams authored Oct 30, 2024
1 parent aae13fd commit 5dd4a00
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
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

0 comments on commit 5dd4a00

Please sign in to comment.