11/*
2- Copyright 2023 API Testing Authors.
2+ Copyright 2023-2025 API Testing Authors.
33
44Licensed under the Apache License, Version 2.0 (the "License");
55you may not use this file except in compliance with the License.
@@ -16,53 +16,56 @@ limitations under the License.
1616package generator
1717
1818import (
19- "bytes"
20- _ "embed"
21- "net/http"
22- "strings"
23- "text/template"
19+ "bytes"
20+ _ "embed"
21+ "net/http"
22+ "strings"
23+ "text/template"
2424
25- "github.com/linuxsuren/api-testing/pkg/testing"
25+ "github.com/linuxsuren/api-testing/pkg/testing"
2626)
2727
2828type curlGenerator struct {
2929}
3030
3131func NewCurlGenerator () CodeGenerator {
32- return & curlGenerator {}
32+ return & curlGenerator {}
3333}
3434
3535func (g * curlGenerator ) Generate (testSuite * testing.TestSuite , testcase * testing.TestCase ) (result string , err error ) {
36- if testcase .Request .Method == "" {
37- testcase .Request .Method = http .MethodGet
38- }
36+ if testcase .Request .Method == "" {
37+ testcase .Request .Method = http .MethodGet
38+ }
3939
40- if ! strings .HasSuffix (testcase .Request .API , "?" ) {
41- testcase .Request .API += "?"
42- }
40+ if ! strings .HasSuffix (testcase .Request .API , "?" ) {
41+ testcase .Request .API += "?"
42+ }
4343
44- queryKeys := testcase .Request .Query .Keys ()
45- for _ , k := range queryKeys {
46- testcase .Request .API += k + "=" + testcase .Request .Query .GetValue (k ) + "&"
47- }
44+ queryKeys := testcase .Request .Query .Keys ()
45+ for _ , k := range queryKeys {
46+ testcase .Request .API += k + "=" + testcase .Request .Query .GetValue (k ) + "&"
47+ }
4848
49- testcase .Request .API = strings .TrimSuffix (testcase .Request .API , "&" )
50- testcase .Request .API = strings .TrimSuffix (testcase .Request .API , "?" )
49+ testcase .Request .API = strings .TrimSuffix (testcase .Request .API , "&" )
50+ testcase .Request .API = strings .TrimSuffix (testcase .Request .API , "?" )
51+ if err = testcase .Request .Render (nil , "" ); err != nil {
52+ return
53+ }
5154
52- var tpl * template.Template
53- if tpl , err = template .New ("curl template" ).Parse (curlTemplate ); err == nil {
54- buf := new (bytes.Buffer )
55- if err = tpl .Execute (buf , testcase ); err == nil {
56- result = strings .TrimSpace (buf .String ())
55+ var tpl * template.Template
56+ if tpl , err = template .New ("curl template" ).Parse (curlTemplate ); err == nil {
57+ buf := new (bytes.Buffer )
58+ if err = tpl .Execute (buf , testcase ); err == nil {
59+ result = strings .TrimSpace (buf .String ())
5760
58- result = strings .ReplaceAll (result , "\n " , " \\ \n " )
59- }
60- }
61- return
61+ result = strings .TrimSuffix (result , " \\ " )
62+ }
63+ }
64+ return
6265}
6366
6467func init () {
65- RegisterCodeGenerator ("curl" , NewCurlGenerator ())
68+ RegisterCodeGenerator ("curl" , NewCurlGenerator ())
6669}
6770
6871//go:embed data/curl.tpl
0 commit comments