- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试这段 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-Type
是 application/json
,但是当我运行该服务并使用 curl 调用它时,Content-Type
是 text/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)
当我改变行与行之间的顺序时,从
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
?
我复制了一个 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}
最佳答案
问题是,正如@colminator 所说,您是在标题之前发送正文。这在任何语言中都行不通 - 这是 HTTP 的事实,而不是 Go 的事实。
它没有被你的测试捕获的原因是你的测试实际上滥用了 ResponseRecorder
;您在 map 中设置字段,然后直接从该 map 读取字段。测试应该只检查 ResponseRecorder.Result
,旨在为您提供客户端实际收到的结果,包括在发送正文时锁定 header :
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")
}
关于go - 使用 httptest 和 curl 的不同内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54682017/
如题,什么时候使用httptest.Server和httptest.ResponseRecorder? 在我看来,我还可以使用 httptest.Server 测试我的处理程序以返回正确的响应。我可以
我想知道如何测试用 fasthttp 编写的应用程序使用 httptest package在 Go 的基础库中。 我找到了 this guide这很好地解释了测试,但问题是 httptest 不满足
当您返回 500 服务器错误时,如何使用 httptest 获取错误消息?注册页面在手动测试时有效,所以这似乎是一些管道问题,但我找不到消息是什么。 func TestSignUp(t *testin
我已经用谷歌搜索了所有内容,但找不到任何东西。 我有一个接受 http.Client 的结构,它会发送多个 GET 请求。在我的测试中,我想模拟响应,这样它就不会发送真正的请求。 目前我已经弄清楚如何
在我的处理程序测试中,我多次使用 header 中带有身份验证 token 的测试请求服务模式。为了对此进行抽象,并为自己节省大量行数,我编写了以下函数: func serveTestReq(payl
一段时间以来,我一直在尝试弄清楚如何为使用上下文作为其定义的一部分的处理程序编写单元测试。 例子 func Handler(ctx context.Context, w http.ResponseWr
我的项目文件夹是这样的。它是一个大型 API,我仅将文件分开用于组织用途。 $ tree src/后端/ src/backend/ ├── cats │ ├── cat_test.go │ ├
我有一些看起来像这样的东西: func (client *MyCustomClient) CheckURL(url string, json_response *MyCustomResponseStr
所以这个真的很奇怪,我正在尝试获得呈现 JSON 的模拟响应。我的测试看起来像这样: import ( "fmt" "net/http" "net/http/httptest"
我目前在我的 LAMP 服务器上设置了一个域,我想添加另一个域。我尝试自己做,但是当我遇到问题时,我遵循 this . 我设置了 example.com,它工作正常,所有流量都会重定向到它的 http
我正在创建一个请求 stub ,以便将其传递给被测试的函数: request := httptest.NewRequest("GET", "http://example.com/foo", nil)
我正在尝试这段 Go 代码 package main import ( "github.com/gorilla/mux" "io" "log" "net/http" )
我喜欢在某些情况下将缓冲的 net/http.ResponseWriter 呈现为 net/http.Response 的能力 net/http/httptest.ResponseRecorder会给
我想知道 this (httptest) 包可用于测试 HTTP/2 特定功能。 谁能给我举一些例子吗? 我知道该工具 h2i ,但它是一个交互式工具。 我正在寻找可编程的东西。 编辑: 我真正想要的
我正在尝试测试我编写的与外部 API 对话的库。我想出了这段代码: import ( "fmt" "net/http" "net/http/httptest" "net
我对 Golang 还是个新手。你对如何在你的 Go 测试文件中有效地创建多个 httptest.NewRequest 有什么想法吗?通常我发起一个新变量两个创建新请求。 例如: r1 := http
如何在隔离单元测试中使用httptest或http包模拟服务器故障? 详细信息: 我一直在使用 gorilla websockets,所以 mt, msg, err := t.conn.ReadMes
我有以下代码: package main import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http"
使用这个 curl 命令,我可以在后端创建零件。请求已成功验证。curl -XPOST -H"Content-Type: application/json" localhost:8080/v1/par
当使用下面的 httptest 服务器测试 https url 的获取请求时,我得到了 http: 服务器给 Golang httptest 中的 HTTPS 客户端的 HTTP 响应。当我使用以“h
我是一名优秀的程序员,十分优秀!