I can't get my test case to equal to the expected result

So I am using the gorilla mux package and I wish I can show the full test case but unfortunately the test case has too many lines of code to even fit it here because the expected result is a big json returned.

I know my expected should be equal to the actual but it seems that when running the unit test it does not work.

func TestHealthCheckHandler(t *testing.T) {
    req, err := http.NewRequest("GET", "/health", nil)

    if err != nil {



    rr := httptest.NewRecorder()
    handler := http.HandlerFunc(HealthCheckHandler)
    handler.ServeHTTP(rr, req)

    if status := rr.Code; status != http.StatusOK {
        t.Errorf("handler returned wrong status code: got %v want %v",
            status, http.StatusOK)

    expected := `{"alive": true}`
    if rr.Body.String() != expected {
        t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)


for a lack of showing my code cause it’s too long, here’s the format for what it should look like. Ideally, I cannot get my expected to equal to rr.Body.String() even though I know they are equal. I am thinking it has something to do with whitespacing… How can I fix this?

Hi @fraud_market,

What I’d first do in such a situation is to write the contents of rr.Body.String() and expected into two files and compare them with a diff tool (or maybe just manually side-by-side, as the difference might be obvious to see).

Alternatively, if white spaces are the only issue, consider “normalizing” the JSON by trimming white space and applying the same indenting to rr.Body.String() and expected before comparing the strings. (See e.g. json.Compact and json.Indent.)