Skip to content

Commit

Permalink
feat: add the cookie setting for golang code generator (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyang1026 authored Apr 14, 2024
1 parent 77526e2 commit e05ff4b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
13 changes: 13 additions & 0 deletions pkg/generator/code_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,23 @@ func TestGenerators(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expectedFormRequestGoCode, result, result)
})

cookieRequest := &atest.TestCase{Request: formRequest.Request}
cookieRequest.Request.Cookie = map[string]string{
"name": "value",
}
t.Run("golang cookie HTTP request", func(t *testing.T) {
result, err := generator.GetCodeGenerator("golang").Generate(nil, cookieRequest)
assert.NoError(t, err)
assert.Equal(t, expectedCookieRequestGoCode, result, result)
})
}

//go:embed testdata/expected_go_code.txt
var expectedGoCode string

//go:embed testdata/expected_go_form_request_code.txt
var expectedFormRequestGoCode string

//go:embed testdata/expected_go_cookie_request_code.txt
var expectedCookieRequestGoCode string
11 changes: 10 additions & 1 deletion pkg/generator/data/main.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
body := bytes.NewBufferString("{{.Request.Body.String}}")
{{- end }}

req, err := http.NewRequest("{{.Request.Method}}," "{{.Request.API}}", body)
req, err := http.NewRequest("{{.Request.Method}}", "{{.Request.API}}", body)
if err != nil {
panic(err)
}
Expand All @@ -25,6 +25,15 @@ func main() {
req.Header.Set("{{$key}}", "{{$val}}")
{{- end}}

{{- if gt (len .Request.Cookie) 0 }}
{{- range $key, $val := .Request.Cookie}}
req.AddCookie(&http.Cookie{
Name: "{{$key}}",
Value: "{{$val}}",
})
{{- end}}
{{- end}}

resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/generator/testdata/expected_go_code.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func main() {
body := bytes.NewBufferString("")

req, err := http.NewRequest("GET," "https://www.baidu.com", body)
req, err := http.NewRequest("GET", "https://www.baidu.com", body)
if err != nil {
panic(err)
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/generator/testdata/expected_go_cookie_request_code.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"io"
"net/http"
)

func main() {
data := url.Values{}
data.Set("key", "value")
body := strings.NewReader(data.Encode())

req, err := http.NewRequest("GET", "https://www.baidu.com", body)
if err != nil {
panic(err)
}
req.Header.Set("User-Agent", "atest")
req.AddCookie(&http.Cookie{
Name: "name",
Value: "value",
})

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))
}
2 changes: 1 addition & 1 deletion pkg/generator/testdata/expected_go_form_request_code.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func main() {
data.Set("key", "value")
body := strings.NewReader(data.Encode())

req, err := http.NewRequest("GET," "https://www.baidu.com", body)
req, err := http.NewRequest("GET", "https://www.baidu.com", body)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit e05ff4b

Please sign in to comment.