Skip to content

Commit af23699

Browse files
committed
add unit tests
1 parent 4cb30e3 commit af23699

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This is a API testing tool.
1616
* Output reference between TestCase
1717
* Run in server mode, and provide the [gRPC endpoint](pkg/server/server.proto)
1818
* [VS Code extension](https://github.com/LinuxSuRen/vscode-api-testing) support
19+
* [HTTP API record](extensions/collector)
1920

2021
## Get started
2122

extensions/collector/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
HTTP API record tool.
2+
3+
## Usage
4+
5+
```shell
6+
atest-collector --filter-path /answer/api/v1
7+
```
8+
9+
It will start a HTTP proxy server, and set the server address to your browser proxy (such as: [SwitchyOmega](https://github.com/FelisCatus/SwitchyOmega)).
10+
11+
`atest-collector` will record all HTTP requests which has prefix `/answer/api/v1`, and
12+
save it to file `sample.yaml` once you close the server.

extensions/collector/cmd/collect.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"os"
88
"os/signal"
9+
"strings"
910
"syscall"
1011

1112
"github.com/elazarl/goproxy"
@@ -37,19 +38,32 @@ func NewRootCmd() (c *cobra.Command) {
3738
return
3839
}
3940

41+
type responseFilter struct {
42+
urlFilter *filter.URLPathFilter
43+
collects *pkg.Collects
44+
}
45+
46+
func (f *responseFilter) filter(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
47+
contentType := resp.Header.Get("Content-Type")
48+
if !strings.Contains(contentType, "application/json") {
49+
return resp
50+
}
51+
52+
req := resp.Request
53+
if f.urlFilter.Filter(req.URL) {
54+
f.collects.Add(req.Clone(context.TODO()))
55+
}
56+
return resp
57+
}
58+
4059
func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
4160
urlFilter := &filter.URLPathFilter{PathPrefix: o.filterPath}
4261
collects := pkg.NewCollects()
62+
responseFilter := &responseFilter{urlFilter: urlFilter, collects: collects}
4363

4464
proxy := goproxy.NewProxyHttpServer()
4565
proxy.Verbose = true
46-
proxy.OnRequest().DoFunc(
47-
func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
48-
if urlFilter.Filter(r.URL) {
49-
collects.Add(r.Clone(context.TODO()))
50-
}
51-
return r, nil
52-
})
66+
proxy.OnResponse().DoFunc(responseFilter.filter)
5367

5468
exporter := pkg.NewSampleExporter()
5569
collects.AddEvent(exporter.Add)
+25-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
package cmd
22

33
import (
4-
"github.com/stretchr/testify/assert"
4+
"net/http"
5+
"net/url"
56
"testing"
7+
8+
"github.com/linuxsuren/api-testing/extensions/collector/pkg"
9+
"github.com/linuxsuren/api-testing/extensions/collector/pkg/filter"
10+
"github.com/stretchr/testify/assert"
611
)
712

813
func TestNewRootCmd(t *testing.T) {
914
c := NewRootCmd()
1015
assert.NotNil(t, c)
1116
assert.Equal(t, "atest-collector", c.Use)
1217
}
18+
19+
func TestResponseFilter(t *testing.T) {
20+
resp := &http.Response{
21+
Header: http.Header{
22+
"Content-Type": []string{"application/json; charset=utf-8"},
23+
},
24+
Request: &http.Request{
25+
URL: &url.URL{},
26+
},
27+
}
28+
emptyResp := &http.Response{}
29+
30+
filter := &responseFilter{
31+
urlFilter: &filter.URLPathFilter{},
32+
collects: pkg.NewCollects(),
33+
}
34+
filter.filter(emptyResp, nil)
35+
filter.filter(resp, nil)
36+
}

0 commit comments

Comments
 (0)