Skip to content

Commit 38942af

Browse files
committed
fix & style: update 09.6.md and 09.7.md
* fix some bugs and typos * optimize presentation and description
1 parent 8096f44 commit 38942af

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

chapter09/09.2.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,4 @@ func main() {
216216
# 导航 #
217217

218218
- 上一节:[testing - 单元测试](09.1.md)
219-
- 下一节:[testing - 子测试](09.3.md)
219+
- 下一节:[testing - 子测试与子基准测试](09.3.md)

chapter09/09.6.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# httptest - HTTP 测试辅助工具 #
22

3-
由于 Go 标准库的强大支持,Go 可以很容易的进行 Web 开发。为此,Go 标准库专门提供了 httptest 包专门用于进行 http Web 开发测试。
3+
由于 Go 标准库的强大支持,Go 可以很容易的进行 Web 开发。为此,Go 标准库专门提供了 `net/http/httptest` 包专门用于进行 http Web 开发测试。
44

55
本节我们通过一个社区帖子的增删改查的例子来学习该包。
66

@@ -21,9 +21,9 @@ type Topic struct {
2121
CreatedAt time.Time `json:"created_at"`
2222
}
2323
```
24-
对于 Topic 的增删改查代码很简单,可以查看[完整代码](code/src/chapter09/httptest/data.go)
24+
对于 `Topic` 的增删改查代码很简单,可以查看[完整代码](/code/src/chapter09/httptest/data.go)
2525

26-
接下来,是通过 http 包来实现一个 Web 应用。
26+
接下来,是通过 `net/http` 包来实现一个 Web 应用。
2727

2828
```go
2929
func main() {
@@ -32,13 +32,13 @@ func main() {
3232
}
3333
...
3434
```
35-
`/topic/` 开头的请求都交由 `handleRequest` 处理,它根据不同的 `Method` 执行相应的增删改查,详细代码可以查看 [server.go](code/src/chapter09/httptest/server.go)
35+
`/topic/` 开头的请求都交由 `handleRequest` 处理,它根据不同的 `Method` 执行相应的增删改查,详细代码可以查看 [server.go](/code/src/chapter09/httptest/server.go)
3636

3737
准备好 Web 应用后,我们启动它。
3838

3939
> go run server.go data.go
4040
41-
通过 curl 进行简单的测试:
41+
通过 `curl` 进行简单的测试:
4242

4343
> 增:curl -i -X POST http://localhost:2017/topic/ -H 'content-type: application/json' -d '{"title":"The Go Standard Library","content":"It contains many packages."}'
4444
@@ -50,7 +50,7 @@ func main() {
5050
5151
## 通过 httptest 进行测试
5252

53-
上面,我们通过 curl 对我们的 Web 应用的接口进行了测试。现在,我们通过 httptest 进行测试
53+
上面,我们通过 `curl` 对我们的 Web 应用的接口进行了测试。现在,我们通过 `net/http/httptest` 包进行测试
5454

5555
我们先测试创建帖子,也就是测试 `handlePost` 函数。
5656

@@ -79,9 +79,9 @@ mux := http.NewServeMux()
7979
mux.HandleFunc("/topic/", handleRequest)
8080
```
8181

82-
因为 `handlePost` 的签名是 `func handlePost(w http.ResponseWriter, r *http.Request) error`,为了测试它,我们必须创建 `http.ResponseWriter``http.Request` 的实例。
82+
因为 `handlePost` 的函数签名是 `func handlePost(w http.ResponseWriter, r *http.Request) error`,为了测试它,我们必须创建 `http.ResponseWriter``http.Request` 的实例。
8383

84-
接下来的代码就是创建一个 `http.Request` 实例 和 一个 `http.ReponseWriter` 的实例。这里的关键是,`httptest` 为我们提供了一个 `http.ReponseWriter` 接口的实现结构:`httptest.ReponseRecorder`,通过它可以得到一个 `http.ReponseWriter`
84+
接下来的代码就是创建一个 `http.Request` 实例 和一个 `http.ResponseWriter` 的实例。这里的关键是,通过 `httptest.NewRecorder()` 可以获得 `httptest.ResponseRecorder` 结构,而此结构实现了`http.ResponseWriter` 接口
8585

8686
```go
8787
reader := strings.NewReader(`{"title":"The Go Standard Library","content":"It contains many packages."}`)
@@ -90,7 +90,7 @@ r, _ := http.NewRequest(http.MethodPost, "/topic/", reader)
9090
w := httptest.NewRecorder()
9191
```
9292

93-
准备好之后,可以测试目标函数了。这里,我们没有直接调用 `handlePost(w, r)`,而是调用 `mux.ServeHTTP(w, r)`,实际上这里直接调用 `handlePost(w, r)` 也是可以的,但调用 `mux.ServeHTTP(w, r)` 更完整的测试了整个流程`mux.ServeHTTP(w, r)` 最终会调用 `handlePost(w, r)`
93+
准备好之后,可以测试目标函数了。这里,我们没有直接调用 `handlePost(w, r)`,而是调用 `mux.ServeHTTP(w, r)`,实际上这里直接调用 `handlePost(w, r)` 也是可以的,但调用 `mux.ServeHTTP(w, r)` 会更完整地测试整个流程`mux.ServeHTTP(w, r)` 最终也会调用到 `handlePost(w, r)`
9494

9595
最后,通过 `go test -v` 运行测试。
9696

@@ -131,7 +131,11 @@ mux := http.NewServeMux()
131131
mux.HandleFunc("/topic/", handleRequest)
132132
```
133133

134-
还有:`w := httptest.NewRecorder()`
134+
以及:
135+
136+
```go
137+
w := httptest.NewRecorder()
138+
```
135139

136140
这正好是前面学习的 `setup` 可以做的事情,因此可以使用 `TestMain` 来做重构。
137141

chapter09/09.7.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# 总结 #
22

3-
除了标准库提供的测试包,还有很多优秀的第三方测试包,比如 [https://github.com/stretchr/testify](https://github.com/stretchr/testify) 用于增强 testing 进行单元测试,进行 mock 等,有机会会写文章介绍它的使用;再比如,[https://github.com/onsi/ginkgo](https://github.com/onsi/ginkgo) 用于集成测试等等
3+
除了标准库提供的测试包,还有很多优秀的第三方测试包,比如 [testify](https://github.com/stretchr/testify) 可有助于 testing 进行单元测试,进行 mock 等,有机会会写文章介绍它的使用;再比如,[ginkgo](https://github.com/onsi/ginkgo) 有助于集成测试等等
44

5-
参考阅读:https://getstream.io/blog/how-we-test-go-at-stream/
5+
参考阅读:[Testing Go at Stream](https://getstream.io/blog/how-we-test-go-at-stream/)
66

77
# 导航 #
88

99
- 上一节:[httptest - HTTP 测试辅助工具](09.6.md)
10-
- [第十章 进程、线程与 goroutine](chapter10/10.0.md)
11-
12-
10+
- [第十章 进程、线程与 goroutine](/chapter10/10.0.md)

0 commit comments

Comments
 (0)