You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repo contains two Go+ classfiles: `yap` (a HTTP Web Framework) and `yaptest` (a HTTP Test Framework).
13
+
14
+
The classfile `yap` has the file suffix `_yap.gox`. And the classfile `yaptest` has the file suffix `_ytest.gox`.
15
+
16
+
Before using `yap` or `yaptest`, you need to add `github.com/goplus/yap` to `go.mod` by using `go get`:
17
+
18
+
```sh
19
+
go get github.com/goplus/yap@latest
20
+
```
21
+
22
+
Then find `require github.com/goplus/yap` statement in `go.mod` and add `//gop:class` at the end of the line:
23
+
24
+
```go.mod
25
+
require github.com/goplus/yap v0.7.2 //gop:class
26
+
```
27
+
10
28
### Router and Parameters
11
29
12
30
demo in Go ([hello.go](demo/hello/hello.go)):
@@ -102,3 +120,53 @@ get "/p/:id", ctx => {
102
120
103
121
run ":8080"
104
122
```
123
+
124
+
### YAP Test Framework
125
+
126
+
Suppose we have a web server named `foo` ([demo/foo/foo_yap.gox](ytest/demo/foo/foo_yap.gox)):
127
+
128
+
```go
129
+
get "/p/:id", ctx => {
130
+
ctx.json {
131
+
"id": ctx.param("id"),
132
+
}
133
+
}
134
+
135
+
run ":8080"
136
+
```
137
+
138
+
Then we create a yaptest file ([demo/foo/foo_ytest.gox](ytest/demo/foo/foo_ytest.gox)):
139
+
140
+
```go
141
+
mock "foo.com", new(foo)
142
+
143
+
run "test get /p/$id", => {
144
+
id:="123"
145
+
get "http://foo.com/p/${id}"
146
+
ret 200
147
+
json {
148
+
"id": id,
149
+
}
150
+
}
151
+
```
152
+
153
+
The directive `mock` creates the `foo` server by [mockhttp](https://pkg.go.dev/github.com/qiniu/x/mockhttp). Then we call the directive `run` to run a subtest.
154
+
155
+
You can change the directive `mock` to `testServer` (see [demo/foo/bar_ytest.gox](ytest/demo/foo/bar_ytest.gox)), and keep everything else unchanged:
156
+
157
+
```go
158
+
testServer "foo.com", new(foo)
159
+
160
+
run "test get /p/$id", => {
161
+
id:="123"
162
+
get "http://foo.com/p/${id}"
163
+
ret 200
164
+
json {
165
+
"id": id,
166
+
}
167
+
}
168
+
```
169
+
170
+
The directive `testServer` creates the `foo` server by [net/http/httptest](https://pkg.go.dev/net/http/httptest#NewServer) and obtained a random port as the service address. Then it calls the directive [host](https://pkg.go.dev/github.com/goplus/yap/ytest#App.Host) to map the random service address to `foo.com`. This makes all other code no need to changed.
171
+
172
+
For more details, see [yaptest - Go+ HTTP Test Framework](ytest).
0 commit comments