-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support generate code from TestCase
- Loading branch information
1 parent
556d58f
commit 4d8bb79
Showing
12 changed files
with
988 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
MIT License | ||
Copyright (c) 2023 API Testing Authors. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package generator | ||
|
||
import "github.com/linuxsuren/api-testing/pkg/testing" | ||
|
||
// CodeGenerator is the interface of code generator | ||
type CodeGenerator interface { | ||
Generate(testcase *testing.TestCase) (result string, err error) | ||
} | ||
|
||
var codeGenerators = map[string]CodeGenerator{} | ||
|
||
func GetCodeGenerator(name string) CodeGenerator { | ||
return codeGenerators[name] | ||
} | ||
|
||
func RegisterCodeGenerator(name string, generator CodeGenerator) { | ||
codeGenerators[name] = generator | ||
} | ||
|
||
func GetCodeGenerators() (result map[string]CodeGenerator) { | ||
// returns an immutable map | ||
result = make(map[string]CodeGenerator, len(codeGenerators)) | ||
for k, v := range codeGenerators { | ||
result[k] = v | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
MIT License | ||
Copyright (c) 2023 API Testing Authors. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package generator_test | ||
|
||
import ( | ||
"testing" | ||
|
||
_ "embed" | ||
|
||
"github.com/linuxsuren/api-testing/pkg/generator" | ||
atest "github.com/linuxsuren/api-testing/pkg/testing" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCodeGeneratorManager(t *testing.T) { | ||
t.Run("GetCodeGenerators", func(t *testing.T) { | ||
// should returns a mutable map | ||
generators := generator.GetCodeGenerators() | ||
count := len(generators) | ||
|
||
generators["a-new-fake"] = nil | ||
assert.Equal(t, count, len(generator.GetCodeGenerators())) | ||
}) | ||
|
||
t.Run("GetCodeGenerator", func(t *testing.T) { | ||
instance := generator.NewGolangGenerator() | ||
generator.RegisterCodeGenerator("fake", instance) | ||
assert.Equal(t, instance, generator.GetCodeGenerator("fake")) | ||
}) | ||
} | ||
|
||
func TestGenerators(t *testing.T) { | ||
testcase := &atest.TestCase{ | ||
Request: atest.Request{ | ||
API: "https://www.baidu.com", | ||
}, | ||
} | ||
t.Run("golang", func(t *testing.T) { | ||
result, err := generator.GetCodeGenerator("golang").Generate(testcase) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedGoCode, result) | ||
}) | ||
} | ||
|
||
//go:embed testdata/expected_go_code.txt | ||
var expectedGoCode string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
{{- if lt (len .Request.Form) 0 }} | ||
data := url.Values{} | ||
{{- range $key, $val := .Request.Form}} | ||
data.Set("$key", "$val") | ||
{{end}} | ||
reader := strings.NewReader(data.Encode()) | ||
{{- else}} | ||
body := bytes.NewBufferString("{{.Request.Body}}") | ||
{{- end }} | ||
|
||
req, err := http.NewRequest("{{.Request.Method}}," "{{.Request.API}}", body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
{{- range $key, $val := .Request.Header}} | ||
req.Header.Set("$key", "$val") | ||
{{end}} | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
panic("status code is not 200") | ||
} | ||
|
||
data, err := io.ReadAll(resp.Body) | ||
println(string(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
MIT License | ||
Copyright (c) 2023 API Testing Authors. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package generator | ||
|
||
import ( | ||
"bytes" | ||
"html/template" | ||
"net/http" | ||
|
||
_ "embed" | ||
|
||
"github.com/linuxsuren/api-testing/pkg/testing" | ||
) | ||
|
||
type golangGenerator struct { | ||
} | ||
|
||
func NewGolangGenerator() CodeGenerator { | ||
return &golangGenerator{} | ||
} | ||
|
||
func (g *golangGenerator) Generate(testcase *testing.TestCase) (result string, err error) { | ||
if testcase.Request.Method == "" { | ||
testcase.Request.Method = http.MethodGet | ||
} | ||
var tpl *template.Template | ||
if tpl, err = template.New("golang template").Parse(golangTemplate); err == nil { | ||
buf := new(bytes.Buffer) | ||
if err = tpl.Execute(buf, testcase); err == nil { | ||
result = buf.String() | ||
} | ||
} | ||
return | ||
} | ||
|
||
func init() { | ||
RegisterCodeGenerator("golang", NewGolangGenerator()) | ||
} | ||
|
||
//go:embed data/main.go.tpl | ||
var golangTemplate string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
body := bytes.NewBufferString("") | ||
|
||
req, err := http.NewRequest("GET," "https://www.baidu.com", body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
panic("status code is not 200") | ||
} | ||
|
||
data, err := io.ReadAll(resp.Body) | ||
println(string(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.