3

I would like to write a unit test for a controller but I keep getting a runtime error. I found out that it is due to the absence of the Host on the request, the ClientIP() method and request body. How can I set them on the test context?

Here is what I got so far. It fails on the line with Host: c.Request.Host.

Controller:

type loggingControllers struct {
    LoggingService services.InterfaceLogging
}

func (l *loggingControllers) RegisterError(c *gin.Context) {
    errorEvent := models.Error{
        Badges:    map[string]string{},
        Host:      c.Request.Host,
        ClientIP:  c.ClientIP(),
        UserAgent: c.Request.UserAgent(),
        Count:     1,
        Timestamp: time.Now().Unix(),
    }

    err := json.NewDecoder(c.Request.Body).Decode(&errorEvent)
    if err != nil {
        utils.RespondWithError(c, http.StatusInternalServerError, err.Error())
        return
    }

    go l.LoggingService.SaveError(errorEvent)

    utils.RespondWithSuccess(c)
}

func GetLoggingControllerMock() loggingControllers {
    loggingServiceMock := services.GetLoggingServiceMock()

    return loggingControllers{
        LoggingService: &loggingServiceMock,
    }
}

Unit Test:

func TestLoggingControllers(t *testing.T) {
    loggingControllers := GetLoggingControllerMock()

    w := httptest.NewRecorder()
    c, _ := gin.CreateTestContext(w)

    loggingControllers.RegisterError(c)
}

Error Message:

--- FAIL: TestLoggingControllers (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x15238ec]
1

1 Answer 1

4

Thanks to Adrian's comment I found the solution.

func TestLoggingControllers(t *testing.T) {
    loggingControllers := GetLoggingControllerMock()

    w := httptest.NewRecorder()
    c, r := gin.CreateTestContext(w)

    r.POST("/", loggingControllers.RegisterError)

    c.Request, _ = http.NewRequest(http.MethodPost, "/", bytes.NewBuffer([]byte("{}")))

    r.ServeHTTP(w, c.Request)

    if w.Code != http.StatusOK {
        t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.