Add Jinja2 templates for test generation
This commit is contained in:
129
templates/go/api_test.go.j2
Normal file
129
templates/go/api_test.go.j2
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
package {{ package_name }}
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
baseURL = "{{ mock_server_url }}"
|
||||||
|
testTimeout = 10 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
{% if security_schemes %}
|
||||||
|
{% for scheme_name, scheme in security_schemes.items() %}
|
||||||
|
{% if scheme.type == "apiKey" %}
|
||||||
|
func get{{ scheme_name|capitalize }}Headers() map[string]string {{
|
||||||
|
return map[string]string{{
|
||||||
|
"{{ scheme.name }}": os.Getenv("API_KEY"),
|
||||||
|
}}
|
||||||
|
}}
|
||||||
|
|
||||||
|
{% elif scheme.type == "http" and scheme.scheme == "bearer" %}
|
||||||
|
func getBearerHeaders() map[string]string {{
|
||||||
|
return map[string]string{{
|
||||||
|
"Authorization": fmt.Sprintf("Bearer %s", os.Getenv("TOKEN")),
|
||||||
|
}}
|
||||||
|
}}
|
||||||
|
|
||||||
|
{% elif scheme.type == "http" and scheme.scheme == "basic" %}
|
||||||
|
func getBasicHeaders(username, password string) map[string]string {{
|
||||||
|
auth := username + ":" + password
|
||||||
|
encoded := base64.StdEncoding.EncodeToString([]byte(auth))
|
||||||
|
return map[string]string{{
|
||||||
|
"Authorization": "Basic " + encoded,
|
||||||
|
}}
|
||||||
|
}}
|
||||||
|
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
func contains(slice []int, item int) bool {{
|
||||||
|
for _, s := range slice {{
|
||||||
|
if s == item {{
|
||||||
|
return true
|
||||||
|
}}
|
||||||
|
}}
|
||||||
|
return false
|
||||||
|
}}
|
||||||
|
|
||||||
|
{% for path, path_endpoints in grouped_endpoints.items() %}
|
||||||
|
{% for endpoint in path_endpoints %}
|
||||||
|
{% set test_name = (endpoint.method + "_" + path.strip("/").replace("/", "_").replace("{", "").replace("}", "")).title().replace("_", "") %}
|
||||||
|
func Test{{ test_name }}(t *testing.T) {{
|
||||||
|
client := &http.Client{{Timeout: testTimeout}}
|
||||||
|
url := baseURL + "{{ path }}"
|
||||||
|
|
||||||
|
{% for param in endpoint.parameters %}
|
||||||
|
{% if param.in == "path" %}
|
||||||
|
url = strings.Replace(url, "{+{{ param.name }}}", "{{ param.name }}", 1)
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
{% if endpoint.method in ["post", "put", "patch"] %}
|
||||||
|
body := `{{`{}`}}`
|
||||||
|
req, err := http.NewRequest("{{ endpoint.method.upper() }}", url, strings.NewReader(body))
|
||||||
|
{% else %}
|
||||||
|
req, err := http.NewRequest("{{ endpoint.method.upper() }}", url, nil)
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
if err != nil {{
|
||||||
|
t.Fatalf("Failed to create request: %v", err)
|
||||||
|
}}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
{% if endpoint.security %}
|
||||||
|
{% set scheme_name = endpoint.security[0].keys()|first %}
|
||||||
|
{% set scheme = security_schemes[scheme_name] %}
|
||||||
|
{% if scheme.type == "apiKey" %}
|
||||||
|
for k, v := range get{{ scheme_name|capitalize }}Headers() {{
|
||||||
|
req.Header.Set(k, v)
|
||||||
|
}}
|
||||||
|
{% elif scheme.type == "http" and scheme.scheme == "bearer" %}
|
||||||
|
for k, v := range getBearerHeaders() {{
|
||||||
|
req.Header.Set(k, v)
|
||||||
|
}}
|
||||||
|
{% endif %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {{
|
||||||
|
t.Fatalf("Request failed: %v", err)
|
||||||
|
}}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if !contains([]int{{200, 201, 204}}, resp.StatusCode) {{
|
||||||
|
t.Errorf("Expected status in [200, 201, 204], got %d", resp.StatusCode)
|
||||||
|
}}
|
||||||
|
|
||||||
|
if resp.StatusCode == 200 || resp.StatusCode == 201 {{
|
||||||
|
var data interface{}
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {{
|
||||||
|
t.Errorf("Failed to decode response: %v", err)
|
||||||
|
}}
|
||||||
|
}}
|
||||||
|
}}
|
||||||
|
|
||||||
|
{% endfor %}
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
func TestAPIHealth(t *testing.T) {{
|
||||||
|
client := &http.Client{{Timeout: testTimeout}}
|
||||||
|
|
||||||
|
resp, err := client.Get(baseURL + "/health")
|
||||||
|
if err != nil {{
|
||||||
|
t.Fatalf("Health check failed: %v", err)
|
||||||
|
}}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != 200 {{
|
||||||
|
t.Errorf("Expected status 200, got %d", resp.StatusCode)
|
||||||
|
}}
|
||||||
|
}}
|
||||||
Reference in New Issue
Block a user