-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathwriter_http.go
More file actions
154 lines (134 loc) · 3.72 KB
/
writer_http.go
File metadata and controls
154 lines (134 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright 2024 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package runner
import (
"bytes"
_ "embed"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"github.com/linuxsuren/api-testing/pkg/apispec"
"github.com/linuxsuren/api-testing/pkg/util"
)
type httpResultWriter struct {
requestMethod string
targetUrl string
parameters map[string]string
templateFile *TemplateOption
}
type TemplateOption struct {
filename string
fileType string
}
// NewHTTPResultWriter creates a new httpResultWriter
func NewHTTPResultWriter(requestType string, url string, parameters map[string]string, templateFile *TemplateOption) ReportResultWriter {
return &httpResultWriter{
requestMethod: requestType,
targetUrl: url,
parameters: parameters,
templateFile: templateFile,
}
}
func NewTemplateOption(filename string, fileType string) *TemplateOption {
return &TemplateOption{
filename: filename,
fileType: fileType,
}
}
// Output writes the JSON base report to target writer
func (w *httpResultWriter) Output(result []ReportResult) (err error) {
url := w.targetUrl
for key, value := range w.parameters {
if url == w.targetUrl {
url = fmt.Sprintf("%s?%s=%s", url, key, value)
} else {
url = fmt.Sprintf("%s&%s=%s", url, key, value)
}
}
log.Println("will send report to:" + url)
var tmpl *template.Template
if w.templateFile == nil {
// use the default template file to serialize the data to JSON format
tmpl, err = template.New("HTTP report template").Parse(defaultTemplate)
if err != nil {
log.Fatalf("Failed to parse template: %v", err)
}
} else {
content, err := os.ReadFile(w.templateFile.filename)
if err != nil {
log.Println("Error reading file:", err)
return err
}
tmpl, err = template.New("HTTP report template").Parse(string(content))
if err != nil {
log.Println("Error parsing template:", err)
return err
}
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, result)
if err != nil {
log.Printf("Failed to render template: %v", err)
return
}
req, err := http.NewRequest(w.requestMethod, url, buf)
if err != nil {
log.Println("Error creating request:", err)
return
}
var contentType string
if w.templateFile != nil {
switch w.templateFile.fileType {
case "html":
contentType = "text/html"
case "yaml":
contentType = "application/yaml"
case "xml":
contentType = "application/xml"
default:
contentType = "application/json"
}
} else {
contentType = "application/json"
}
req.Header.Set(util.ContentType, contentType)
var resp *http.Response
if resp, err = http.DefaultClient.Do(req); err != nil {
log.Println("error when client do", err)
return
}
if resp.StatusCode == http.StatusOK {
var data []byte
if data, err = io.ReadAll(resp.Body); err != nil {
log.Println("error when ReadAll", err)
return
}
log.Println("getting response back", data)
}
return
}
//go:embed writer_templates/example.tpl
var defaultTemplate string
// WithAPIConverage sets the api coverage
func (w *httpResultWriter) WithAPICoverage(apiConverage apispec.APICoverage) ReportResultWriter {
return w
}
func (w *httpResultWriter) WithResourceUsage([]ResourceUsage) ReportResultWriter {
return w
}
func (w *httpResultWriter) GetWriter() io.Writer {
return nil
}