Skip to content

Commit 8da0890

Browse files
authored
feat: add a HTTP request collector as atest extension (#85)
* feat: add a HTTP request collector as atest extension * add unit tests for collector * add unit tests * support to release atest-collector via goreleaser --------- Co-authored-by: Rick <[email protected]>
1 parent b0b9bf8 commit 8da0890

File tree

22 files changed

+634
-18
lines changed

22 files changed

+634
-18
lines changed

.github/workflows/build.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
- name: Test
1717
run: |
1818
go test ./... -coverprofile coverage.out
19+
make test test-collector
1920
- name: Report
2021
if: github.actor == 'linuxsuren'
2122
env:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
bin/
22
.idea/
33
coverage.out
4+
collector-coverage.out
45
dist/
56
.vscode/launch.json
7+
sample.yaml

.goreleaser.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ before:
77
builds:
88
- env:
99
- CGO_ENABLED=0
10+
id: atest
1011
binary: atest
1112
goos:
1213
- linux
@@ -16,8 +17,31 @@ builds:
1617
- -w
1718
- -s
1819
- -X github.com/linuxsuren/api-testing/pkg/version.version={{.Version}}
20+
- env:
21+
- CGO_ENABLED=0
22+
id: collector
23+
binary: atest-collector
24+
main: ./extensions/collector/main.go
25+
goos:
26+
- linux
27+
- windows
28+
- darwin
29+
ldflags:
30+
- -w
31+
- -s
1932
archives:
2033
- name_template: "{{ .Binary }}-{{ .Os }}-{{ .Arch }}"
34+
builds:
35+
- atest
36+
format_overrides:
37+
- goos: windows
38+
format: zip
39+
files:
40+
- README.md
41+
- name_template: "{{ .Binary }}-{{ .Os }}-{{ .Arch }}"
42+
id: collector
43+
builds:
44+
- collector
2145
format_overrides:
2246
- goos: windows
2347
format: zip

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ copy-restart: build
1717
test:
1818
go test ./... -cover -v -coverprofile=coverage.out
1919
go tool cover -func=coverage.out
20+
test-collector:
21+
go test github.com/linuxsuren/api-testing/extensions/collector/./... -cover -v -coverprofile=collector-coverage.out
22+
go tool cover -func=collector-coverage.out
2023
grpc:
2124
protoc --go_out=. --go_opt=paths=source_relative \
2225
--go-grpc_out=. --go-grpc_opt=paths=source_relative \

README.md

Lines changed: 1 addition & 0 deletions
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/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test:
2+
go test ./... -cover -v -coverprofile=coverage.out
3+
go tool cover -func=coverage.out

extensions/collector/README.md

Lines changed: 12 additions & 0 deletions
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.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"os"
8+
"os/signal"
9+
"strings"
10+
"syscall"
11+
12+
"github.com/elazarl/goproxy"
13+
"github.com/linuxsuren/api-testing/extensions/collector/pkg"
14+
"github.com/linuxsuren/api-testing/extensions/collector/pkg/filter"
15+
"github.com/spf13/cobra"
16+
)
17+
18+
type option struct {
19+
port int
20+
filterPath string
21+
output string
22+
}
23+
24+
// NewRootCmd creates the root command
25+
func NewRootCmd() (c *cobra.Command) {
26+
opt := &option{}
27+
c = &cobra.Command{
28+
Use: "atest-collector",
29+
Short: "A collector for API testing, it will start a HTTP proxy server",
30+
RunE: opt.runE,
31+
}
32+
flags := c.Flags()
33+
flags.IntVarP(&opt.port, "port", "p", 8080, "The port for the proxy")
34+
flags.StringVarP(&opt.filterPath, "filter-path", "", "", "The path prefix for filtering")
35+
flags.StringVarP(&opt.output, "output", "o", "sample.yaml", "The output file")
36+
37+
_ = cobra.MarkFlagRequired(flags, "filter-path")
38+
return
39+
}
40+
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+
59+
func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
60+
urlFilter := &filter.URLPathFilter{PathPrefix: o.filterPath}
61+
collects := pkg.NewCollects()
62+
responseFilter := &responseFilter{urlFilter: urlFilter, collects: collects}
63+
64+
proxy := goproxy.NewProxyHttpServer()
65+
proxy.Verbose = true
66+
proxy.OnResponse().DoFunc(responseFilter.filter)
67+
68+
exporter := pkg.NewSampleExporter()
69+
collects.AddEvent(exporter.Add)
70+
71+
srv := &http.Server{
72+
Addr: fmt.Sprintf(":%d", o.port),
73+
Handler: proxy,
74+
}
75+
76+
sig := make(chan os.Signal, 1)
77+
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
78+
go func() {
79+
<-sig
80+
collects.Stop()
81+
_ = srv.Shutdown(context.Background())
82+
}()
83+
84+
cmd.Println("Starting the proxy server with port", o.port)
85+
_ = srv.ListenAndServe()
86+
var data string
87+
if data, err = exporter.Export(); err == nil {
88+
err = os.WriteFile(o.output, []byte(data), 0644)
89+
}
90+
return
91+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cmd
2+
3+
import (
4+
"net/http"
5+
"net/url"
6+
"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"
11+
)
12+
13+
func TestNewRootCmd(t *testing.T) {
14+
c := NewRootCmd()
15+
assert.NotNil(t, c)
16+
assert.Equal(t, "atest-collector", c.Use)
17+
}
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+
}

extensions/collector/go.mod

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module github.com/linuxsuren/api-testing/extensions/collector
2+
3+
go 1.19
4+
5+
require (
6+
github.com/elazarl/goproxy v0.0.0-20221015165544-a0805db90819
7+
github.com/linuxsuren/api-testing v0.0.11
8+
github.com/spf13/cobra v1.7.0
9+
github.com/stretchr/testify v1.8.4
10+
gopkg.in/yaml.v2 v2.4.0
11+
)
12+
13+
require (
14+
github.com/Masterminds/goutils v1.1.1 // indirect
15+
github.com/Masterminds/semver/v3 v3.2.0 // indirect
16+
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
17+
github.com/davecgh/go-spew v1.1.1 // indirect
18+
github.com/ghodss/yaml v1.0.0 // indirect
19+
github.com/google/uuid v1.3.0 // indirect
20+
github.com/huandu/xstrings v1.3.3 // indirect
21+
github.com/imdario/mergo v0.3.11 // indirect
22+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
23+
github.com/kr/pretty v0.1.0 // indirect
24+
github.com/mitchellh/copystructure v1.0.0 // indirect
25+
github.com/mitchellh/reflectwalk v1.0.0 // indirect
26+
github.com/pmezard/go-difflib v1.0.0 // indirect
27+
github.com/shopspring/decimal v1.2.0 // indirect
28+
github.com/spf13/cast v1.3.1 // indirect
29+
github.com/spf13/pflag v1.0.5 // indirect
30+
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
31+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
32+
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
33+
golang.org/x/crypto v0.3.0 // indirect
34+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
35+
gopkg.in/yaml.v3 v3.0.1 // indirect
36+
)
37+
38+
replace github.com/linuxsuren/api-testing => ../../.

0 commit comments

Comments
 (0)