This article demonstrates how to test the POST API endpoint for creating a new product, covering helper functions, request construction, and response verification.
In this article, we demonstrate how to test the POST API endpoint for creating a new product. The guide covers writing helper functions, constructing the request payload, sending the request, and verifying the API response.
First, we define helper functions to verify status codes and to send HTTP requests. These functions simplify the testing process by encapsulating common operations.
The test for the POST API for creating a product involves several key steps:
Clearing the table: Ensure the database table is empty before running the test.
Preparing the payload: A JSON payload representing a new product is created.
Creating and sending the HTTP POST request: The JSON payload is converted into a byte slice and wrapped in a bytes buffer, as required by the http.NewRequest function.
Verifying the response: The response status is checked to confirm if the product was created successfully.
The request header is explicitly set to "application/json" to ensure the server processes the payload correctly.
After executing the request, we inspect the response payload by unmarshaling the JSON into a map. This verification ensures that the product details in the response match the expected values. Note that when JSON is unmarshaled into a map with interface{} values, numbers convert to float64 by default.
Copy
Ask AI
func TestCreateProduct(t *testing.T) { clearTable() var product = []byte(`{"name":"chair", "quantity":1, "price":100}`) req, _ := http.NewRequest("POST", "/product", bytes.NewBuffer(product)) req.Header.Set("Content-Type", "application/json") response := sendRequest(req) checkStatusCode(t, http.StatusCreated, response.Code) var m map[string]interface{} json.Unmarshal(response.Body.Bytes(), &m) if m["name"] != "chair" { t.Errorf("Expected name: %v, Got: %v", "chair", m["name"]) } if m["quantity"] != 1.0 { // Numbers are unmarshaled as float64. t.Errorf("Expected quantity: %v, Got: %v", 1.0, m["quantity"]) }}
For additional context, here is an excerpt from the API implementation where the createProduct method is defined. This method inserts the new product into the database and sends the appropriate HTTP response:
An important detail is how JSON numbers are unmarshaled. Initially, an error occurred because the expected quantity was specified as 1 (an integer) while the unmarshaled value was 1.0 (a float64). Adjusting the expected value to 1.0 resolved the issue, and the test passed successfully.
Copy
Ask AI
go test2022/12/28 05:50:34 ClearTable2022/12/28 05:50:34 ClearTable2022/12/28 05:50:34 Float64PASSok example.com/my-inventory 0.469s
This article demonstrated how to test the POST API for creating a product. We covered the creation of helper functions, setting up the test request, verifying the response, and handling JSON number conversion in Go.Next, we will explore testing the DELETE API. Stay tuned for more detailed tutorials on API testing.