Skip to content
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
95 changes: 64 additions & 31 deletions fluentrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -17,43 +18,75 @@ const (
expectedPATCHBody = "{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"foo\",\n \"body\": \"bar\"\n}"
)

func TestRequest(t *testing.T) {
resp, err := FluentRequest().
Method("GET").
Url("https://jsonplaceholder.typicode.com/todos/1").
Run()

body, _ := ioutil.ReadAll(resp.Body)

assert.NoError(t, err)
assert.Equal(t, expectedBody, string(body))
assert.Equal(t, http.StatusOK, resp.StatusCode)
type ExpectedResult struct {
body string
status int
}

func TestPOST(t *testing.T) {
data, _ := json.Marshal(map[string]interface{}{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false,
})

header := http.Header{
"Content-Type": {"application/json; charset=UTF-8"},
func TestRequests(t *testing.T) {
var tests = []struct {
name string
options map[string]interface{}
expectedResult ExpectedResult
}{
{
name: "testGet",
options: map[string]interface{}{
"Method": "GET",
"Url": "https://jsonplaceholder.typicode.com/todos/1",
},
expectedResult: ExpectedResult{
body: `{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}`,
status: http.StatusOK,
},
},
{
name: "testPost",
options: map[string]interface{}{
"Method": "POST",
"Url": "https://jsonplaceholder.typicode.com/posts",
"Body": bytes.NewBuffer([]byte(`{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}`)),
},
expectedResult: ExpectedResult{
body: `{"id": 101}`,
status: http.StatusCreated,
},
},
{
name: "testPostwithJsonHeader",
options: map[string]interface{}{
"Method": "POST",
"Url": "https://jsonplaceholder.typicode.com/posts",
"Body": bytes.NewBuffer([]byte(`{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}`)),
"Header": http.Header{"Content-Type": {"application/json; charset=UTF-8"}},
},
expectedResult: ExpectedResult{
body: `{"completed": false,"id": 101,"title": "delectus aut autem","userId": 1}`,
status: http.StatusCreated,
},
},
}

resp, err := FluentRequest().
Method(http.MethodPost).
Url("https://jsonplaceholder.typicode.com/posts").
Body(bytes.NewBuffer(data)).
Header(header).
Run()
for _, e := range tests {
t.Run(e.name, func(t *testing.T) {
f := FluentRequest()
for method, parameter := range e.options {
meth := reflect.ValueOf(f).MethodByName(method)
param := reflect.ValueOf(parameter)
resp := meth.Call([]reflect.Value{param})

body, _ := ioutil.ReadAll(resp.Body)
f = resp[0].Interface().(*fluentRequest)
}

assert.NoError(t, err)
assert.Equal(t, expectedPOSTBody, string(body))
assert.Equal(t, http.StatusCreated, resp.StatusCode)
resp, err := f.Run()

body, _ := ioutil.ReadAll(resp.Body)

assert.NoError(t, err)
assert.Equal(t, e.expectedResult.status, resp.StatusCode)
assert.JSONEq(t, e.expectedResult.body, string(body))
})
}
}

func TestPUT(t *testing.T) {
Expand Down