@@ -3,19 +3,15 @@ package cmd
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "io"
7
6
"net/http"
8
7
"os"
9
8
"os/signal"
10
- "strings"
11
9
"syscall"
12
10
13
11
"github.com/elazarl/goproxy"
14
12
"github.com/linuxsuren/api-testing/extensions/collector/pkg"
15
13
"github.com/linuxsuren/api-testing/extensions/collector/pkg/filter"
16
- atestpkg "github.com/linuxsuren/api-testing/pkg/testing"
17
14
"github.com/spf13/cobra"
18
- "gopkg.in/yaml.v2"
19
15
)
20
16
21
17
type option struct {
@@ -24,6 +20,7 @@ type option struct {
24
20
output string
25
21
}
26
22
23
+ // NewRootCmd creates the root command
27
24
func NewRootCmd () (c * cobra.Command ) {
28
25
opt := & option {}
29
26
c = & cobra.Command {
@@ -36,7 +33,7 @@ func NewRootCmd() (c *cobra.Command) {
36
33
flags .StringVarP (& opt .filterPath , "filter-path" , "" , "" , "The path prefix for filtering" )
37
34
flags .StringVarP (& opt .output , "output" , "o" , "sample.yaml" , "The output file" )
38
35
39
- cobra .MarkFlagRequired (flags , "filter-path" )
36
+ _ = cobra .MarkFlagRequired (flags , "filter-path" )
40
37
return
41
38
}
42
39
@@ -54,12 +51,8 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
54
51
return r , nil
55
52
})
56
53
57
- exporter := & sampleExporter {
58
- testSuite : atestpkg.TestSuite {
59
- Name : "sample" ,
60
- },
61
- }
62
- collects .AddEvent (exporter .add )
54
+ exporter := pkg .NewSampleExporter ()
55
+ collects .AddEvent (exporter .Add )
63
56
64
57
srv := & http.Server {
65
58
Addr : fmt .Sprintf (":%d" , o .port ),
@@ -71,69 +64,14 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
71
64
go func () {
72
65
<- sig
73
66
collects .Stop ()
74
- srv .Shutdown (context .Background ())
67
+ _ = srv .Shutdown (context .Background ())
75
68
}()
76
69
77
70
cmd .Println ("Starting the proxy server with port" , o .port )
78
- srv .ListenAndServe ()
71
+ _ = srv .ListenAndServe ()
79
72
var data string
80
- if data , err = exporter .export (); err == nil {
73
+ if data , err = exporter .Export (); err == nil {
81
74
err = os .WriteFile (o .output , []byte (data ), 0644 )
82
75
}
83
76
return
84
77
}
85
-
86
- type sampleExporter struct {
87
- testSuite atestpkg.TestSuite
88
- }
89
-
90
- func (e * sampleExporter ) add (r * http.Request ) {
91
- body := r .Body
92
- data , _ := io .ReadAll (body )
93
-
94
- fmt .Println ("receive" , r .URL .Path )
95
- req := atestpkg.Request {
96
- API : r .URL .String (),
97
- Method : r .Method ,
98
- Header : map [string ]string {},
99
- Body : string (data ),
100
- }
101
-
102
- testCase := atestpkg.TestCase {
103
- Request : req ,
104
- Expect : atestpkg.Response {
105
- StatusCode : 200 ,
106
- },
107
- }
108
-
109
- specs := strings .Split (r .URL .Path , "/" )
110
- if len (specs ) > 0 {
111
- testCase .Name = specs [len (specs )- 1 ]
112
- }
113
-
114
- if val := r .Header .Get ("Content-Type" ); val != "" {
115
- req .Header ["Content-Type" ] = val
116
- }
117
-
118
- e .testSuite .Items = append (e .testSuite .Items , testCase )
119
- }
120
-
121
- var prefix = `#!api-testing
122
- # yaml-language-server: $schema=https://gitee.com/linuxsuren/api-testing/raw/master/sample/api-testing-schema.json
123
- `
124
-
125
- func (e * sampleExporter ) export () (string , error ) {
126
- marker := map [string ]int {}
127
-
128
- for i , item := range e .testSuite .Items {
129
- if _ , ok := marker [item .Name ]; ok {
130
- marker [item .Name ]++
131
- e .testSuite .Items [i ].Name = fmt .Sprintf ("%s-%d" , item .Name , marker [item .Name ])
132
- } else {
133
- marker [item .Name ] = 0
134
- }
135
- }
136
-
137
- data , err := yaml .Marshal (e .testSuite )
138
- return prefix + string (data ), err
139
- }
0 commit comments