Go - Testing a Web Application or a Web Service

发布时间 2023-10-18 19:11:11作者: ZhangZhihuiAAA

Problem: You want to do unit testing on a web application or a web service.


Solution: Use the httptest.NewRecorder function to create an httptest.ResponseRecorder that can be used to record what’s been written to the http.ResponseWriter . This can then be used to test the response.

 

Web applications and web services are the most popular type of programs written in Go, so obviously you need to be able to test them. The httptest package provides a way to do this.

The handler function is the function that is called when a request is received. The handler function writes the response to the http.ResponseWriter , which is then passed back to the client. This approach focuses on testing the handler function that is used to handle the request, which covers most of what you need. Specific functions can be tested using the normal approach, but testing the handler is a bit tricky because you need to create an HTTP server to test it.

The httptest.NewRecorder function creates an httptest.ResponseRecorder that implements the http.ResponseWriter interface. This can be used to record what’s been written to the http.ResponseWriter so that you can test the response:

Let's say we have this handler function we want to test:

func hello(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello  World!")
}

You can test this using the httptest package like this:

func TestHttpHello(t *testing.T) {
    http.HandleFunc("/hello", hello)
    writer := httptest.NewRecorder()
    request, _ := http.NewRequest("GET", "/hello", nil)
    http.DefaultServeMux.ServeHTTP(writer, request)
    if writer.Code != http.StatusOK {
        t.Errorf("Response  code  is  %v", writer.Code)
    }
    if expected, actual := "Hello  World!", writer.Body.String(); expected != actual {
        t.Errorf("Response  body  is  %v", actual)
    }
}

First, you need to register the handler function with the http.DefaultServeMux using the http.HandleFunc function. This is the same way you would register the handler function normally. Then you create an httptest.ResponseRecorder using the httptest.NewRecorder function. This implements the http.ResponseWriter interface.

Next, you need to create the client to emulate someone sending an HTTP request to the server. Use the http.NewRequest function to create a GET request to the /hello URL. With this, you have both the http.ResponseWriter and http.Request that you can pass to ServeHTTP to dispatch the request to the handler function that matches the URL. Remember, when you registered the handler function using http.HandleFunc , you actually registered the function to http.DefaultServeMux . This is the same multiplexer you are using to dispatch the request.

Once the request is dispatched, the response is written to the ht⁠tpt⁠est.⁠Resp⁠onseRe⁠cor⁠der that you created. You can then test the response code and the response body to make sure that the response is what you expect.

The httptest.ResponseRecorder has a Code field that contains the HTTP status code that was written to the http.ResponseWriter . You can test this to make sure it’s the expected value. The httptest.ResponseRecorder also has a Body field that contains the response body that was written to the http.ResponseWriter . You can test this as well to make sure it’s the expected value. You can also test for other parts of the response, such as the headers, cookies, etc.