Chapter 7 – Testing Gin HTTP Routes
- Define a
TestUpdateRecipeHandler
inmain_test.go
as follows:func TestUpdateRecipeHandler(t *testing.T) { ts := httptest.NewServer(SetupServer()) defer ts.Close() recipe := Recipe{ ID: "c0283p3d0cvuglq85log", Name: "Oregano Marinated Chicken", } raw, _ := json.Marshal(recipe) resp, err := http.PUT(fmt.Sprintf("%s/recipes/%s", ts.URL, recipe.ID), bytes.NewBuffer(raw)) defer resp.Body.Close() assert.Nil(t, err) assert.Equal(t, http.StatusOK, resp.StatusCode) data, _ := ioutil.ReadAll(resp.Body) var payload map[string]string json.Unmarshal(data, &payload) ...