From 10792efb7b7c6579a697c0daf1189e70a9b72e7f Mon Sep 17 00:00:00 2001 From: Vitor Estevam Date: Wed, 5 Oct 2022 10:59:57 -0300 Subject: [PATCH] adding Dinamic Tests --- fluentrequest_test.go | 95 +++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 31 deletions(-) diff --git a/fluentrequest_test.go b/fluentrequest_test.go index e940669..7e97554 100644 --- a/fluentrequest_test.go +++ b/fluentrequest_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "io/ioutil" "net/http" + "reflect" "testing" "github.com/stretchr/testify/assert" @@ -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) {