dongxia9620 2017-07-24 08:02
浏览 58
已采纳

缓冲区string()不等于字符串

I try to test if my http handler is returning the right values inside the body.

This is my handler function

func Index(w http.ResponseWriter, r *http.Request){
    message := `{"status": "OK"}`
    w.WriteHeader(http.StatusOK)
    w.Header().Set("Content-Type", "application/json;charset=UTF-8")
    if err := json.NewEncoder(w).Encode(message); err != nil {
            panic(err)
    }
}

This is my test

func TestIndex(t *testing.T){

    req, err := http.NewRequest("GET", "/", nil)
    if err != nil {
            t.Fatal(err)
    }

    rr := httptest.NewRecorder()
    handler := http.HandlerFunc(Index)

    handler.ServeHTTP(rr, req)

    expected := `"{"status": "OK"}"`

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

The result of the test is:

handlers_test.go:244: handler returned unexpected body: got "{\"status\": \"OK\"}"
     want {"status": "OK"}

I think the escaping of the " is because of the json Encode, but even if I change the expected to

expected := `"{\"status\": \"OK\"}"`

it does not work, than the result of the test is

handlers_test.go:244: handler returned unexpected body: got "{\"status\": \"OK\"}"
     want "{\"status\": \"OK\"}"

In the docs I found something that json encode appends a newline character, but I did not manage to get the test working even with that information :-/

In advance, thanks for your help.

  • 写回答

2条回答 默认 最新

  • dongshuo5101 2017-07-24 08:07
    关注

    The message you send:

    message := `{"status": "OK"}`
    

    Is already a valid JSON text, you don't need any further JSON encoding / processing on it. Just send it as-is:

    func Index(w http.ResponseWriter, r *http.Request){
        message := `{"status": "OK"}`
        w.Header().Set("Content-Type", "application/json;charset=UTF-8")
        io.WriteString(w, message)
    }
    

    Also note that if your response code is http.StatusOK, you don't need to set that explicitly, as that is the default if you don't set it.

    And then simply expect the following response:

    expected := `{"status": "OK"}`
    

    Explaining your original code:

    In your original code you JSON-marshaled a single string value, whose content was {"status": "OK"}. JSON encoding will quote this text into a valid JSON string (prefix quotation marks with a backslash), put inside quotes, and also appends a newline character. So this becomes the raw string:

    expected := `"{\"status\": \"OK\"}"
    `
    

    Using this expected value, your test passes, but again, what you want is in the first part of this answer.

    If you want to use an interpreted string literal to describe the expected value, this is how it could look like:

    expected := "\"{\\\"status\\\": \\\"OK\\\"}\"
    "
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog