2

我正在尝试这个 Go 代码

package main

import (
    "github.com/gorilla/mux"
    "io"
    "log"
    "net/http"
)

func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
    w.Header().Set("Content-Type", "application/json")

    io.WriteString(w, `{"alive": true}`)
}

func main() {

    router := mux.NewRouter()
    router.HandleFunc("/health", HealthCheckHandler).Methods("GET")

    log.Printf("running server ...")
    log.Fatal(http.ListenAndServe(":8000", router))
}

通过这个测试

package main

import (
    "net/http"
    "net/http/httptest"
    "testing"
)

func TestHealthCheckHandler(t *testing.T) {
    req, err := http.NewRequest("GET", "/health", nil)
    if err != nil {
        t.Fatal(err)
    }
    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)
    }

    t.Logf("%v", rr.Header())

    if ctype := rr.Header().Get("Content-Type"); ctype != "application/json" {
        t.Errorf("content type header does not match: got %v want %v",
            ctype, "application/json")
    }
}

当我运行测试时,一切正常

go test -v
=== RUN   TestHealthCheckHandler
--- PASS: TestHealthCheckHandler (0.00s)
    handlers_test.go:24: map[Content-Type:[application/json]]
PASS
ok          0.012s

Content-Typeis ,application/json但是当我运行服务并用 curl 调用它时,Content-Typeistext/plain

curl -v localhost:8000/health
*   Trying ::1...
* Connected to localhost (::1) port 8000 (#0)
> GET /health HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Thu, 14 Feb 2019 01:37:15 GMT
< Content-Length: 15
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host localhost left intact

为什么行为与测试和执行不同?

(示例基于https://github.com/gorilla/mux#testing-handlers

编辑 1

当我更改行之间的顺序时,从

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

使用 curl,我得到了预期的行为,Content-Type: application/json

curl -v localhost:8000/health
*   Trying ::1...
* Connected to localhost (::1) port 8000 (#0)
> GET /health HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Thu, 14 Feb 2019 01:43:18 GMT
< Content-Length: 15
<
* Connection #0 to host localhost left intact

但无论如何,在原始情况下,为什么测试和执行显示不同Content-Type

编辑 2

我复制了一个 tcpdump,它包含Content-Type: text/plain.

11:51:05.686149 IP localhost.47368 > localhost.32000: Flags [P.], seq 1:92, ack 1, win 342, options [nop,nop,TS val 23544153 ecr 23544153], length 91
E...-L@.@.............}..Vl..WAu...V.......
.gAY.gAYGET /health-check HTTP/1.1
Host: localhost:32000
User-Agent: curl/7.47.0
Accept: */*


11:51:05.686847 IP localhost.32000 > localhost.47368: Flags [P.], seq 1:133, ack 92, win 342, options [nop,nop,TS val 23544153 ecr 23544153], length 132
E....-@.@.<.........}....WAu.Vln...V.......
.gAY.gAYHTTP/1.1 200 OK
Date: Thu, 14 Feb 2019 14:51:05 GMT
Content-Length: 15
Content-Type: text/plain; charset=utf-8

{"alive": true}

4

2 回答 2

2

问题是,正如@colminator 所说,您在标题之前发送了您的正文。这在任何语言中都不起作用——这是 HTTP 的事实,而不是 Go 的事实。

它没有被您的测试捕获的原因是您的测试实际上是在滥用ResponseRecorder;您正在地图中设置字段,然后直接从该地图中读取字段。测试应该只检查ResponseRecorder.Result,它旨在为您提供客户端实际收到的结果,包括在发送正文时锁定标头:

if ctype := rr.Response().Header.Get("Content-Type"); ctype != "application/json" {
    t.Errorf("content type header does not match: got %v want %v",
        ctype, "application/json")
}
于 2019-02-14T15:04:09.433 回答
2

只需将您的功能更改为:

func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {

    // this will cause a duplicate status header to be written
    // w.WriteHeader(http.StatusOK)

    w.Header().Set("Content-Type", "application/json")

    io.WriteString(w, `{"alive": true}`)
}
于 2019-02-14T02:08:18.957 回答