-2

I want to test the API function but arguments give the problem.

 func SetAPIConfigHandler(w http.ResponseWriter, r *http.Request) {
    var apiConfig model.Configuration
    err := json.NewDecoder(r.Body).Decode(&apiConfig)
    if err != nil {
        responderror(w, http.StatusBadRequest, err.Error())
    } else {
        utils.Domain = apiConfig.Domain
        utils.BaseURL = apiConfig.BaseURL
        utils.Tenant = apiConfig.Tenant
        respondJSON(w, http.StatusOK, apiConfig)
    }
}
dlsniper
  • 7,188
  • 1
  • 35
  • 44
  • 2
    Look into the [`net/http/httptest`](https://golang.org/pkg/net/http/httptest/) package which provides the means to (easily) test handlers. – icza Jun 17 '21 at 08:40

1 Answers1

2

You can test it using httptest, something like this:

    req := httptest.NewRequest("GET", "http://example.com/foo", nil)
    w := httptest.NewRecorder()
    SetAPIConfigHandler(w, req)

    resp := w.Result()
    body, _ := io.ReadAll(resp.Body)

    fmt.Println(resp.StatusCode)
    fmt.Println(resp.Header.Get("Content-Type"))
    fmt.Println(string(body))
Jakub Dóka
  • 2,477
  • 1
  • 7
  • 12