Skip to content

Commit ad3ee24

Browse files
authored
feat: Add HTTP method testing endpoints (#14)
2 parents 3fd9189 + 7f080b3 commit ad3ee24

File tree

2 files changed

+227
-0
lines changed

2 files changed

+227
-0
lines changed

cmd/server/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/speakeasy-api/speakeasy-api-test-service/internal/clientcredentials"
1010
"github.com/speakeasy-api/speakeasy-api-test-service/internal/errors"
1111
"github.com/speakeasy-api/speakeasy-api-test-service/internal/eventstreams"
12+
"github.com/speakeasy-api/speakeasy-api-test-service/internal/method"
1213
"github.com/speakeasy-api/speakeasy-api-test-service/internal/middleware"
1314
"github.com/speakeasy-api/speakeasy-api-test-service/internal/pagination"
1415
"github.com/speakeasy-api/speakeasy-api-test-service/internal/readonlywriteonly"
@@ -58,6 +59,14 @@ func main() {
5859
r.HandleFunc("/clientcredentials/alt/token", clientcredentials.HandleTokenRequest).Methods(http.MethodPost)
5960
r.HandleFunc("/clientcredentials/alt/authenticatedrequest", clientcredentials.HandleAuthenticatedRequest).Methods(http.MethodPost)
6061
r.HandleFunc("/reflect", reflect.HandleReflect).Methods(http.MethodPost)
62+
r.HandleFunc("/method/delete", method.HandleDelete).Methods(http.MethodDelete)
63+
r.HandleFunc("/method/get", method.HandleGet).Methods(http.MethodGet)
64+
r.HandleFunc("/method/head", method.HandleHead).Methods(http.MethodHead)
65+
r.HandleFunc("/method/options", method.HandleOptions).Methods(http.MethodOptions)
66+
r.HandleFunc("/method/patch", method.HandlePatch).Methods(http.MethodPatch)
67+
r.HandleFunc("/method/post", method.HandlePost).Methods(http.MethodPost)
68+
r.HandleFunc("/method/put", method.HandlePut).Methods(http.MethodPut)
69+
r.HandleFunc("/method/trace", method.HandleTrace).Methods(http.MethodTrace)
6170

6271
handler := middleware.Fault(r)
6372

internal/method/service.go

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
package method
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
"net/http"
7+
8+
"github.com/speakeasy-api/speakeasy-api-test-service/internal/utils"
9+
)
10+
11+
func HandleDelete(w http.ResponseWriter, r *http.Request) {
12+
type RequestBody struct {
13+
ID string `json:"id"`
14+
}
15+
type ResponseBody struct {
16+
Status string `json:"status"`
17+
}
18+
19+
var requestBody RequestBody
20+
21+
body, err := io.ReadAll(r.Body)
22+
23+
if err != nil {
24+
utils.HandleError(w, err)
25+
return
26+
}
27+
28+
if err := json.Unmarshal(body, &requestBody); err != nil {
29+
utils.HandleError(w, err)
30+
return
31+
}
32+
33+
w.Header().Set("Content-Type", "application/json")
34+
w.WriteHeader(http.StatusOK)
35+
36+
responseBody := ResponseBody{
37+
Status: "OK",
38+
}
39+
40+
if err := json.NewEncoder(w).Encode(responseBody); err != nil {
41+
utils.HandleError(w, err)
42+
return
43+
}
44+
}
45+
46+
func HandleGet(w http.ResponseWriter, r *http.Request) {
47+
type RequestBody struct {
48+
ID string `json:"id"`
49+
}
50+
type ResponseBody struct {
51+
Status string `json:"status"`
52+
}
53+
54+
var requestBody RequestBody
55+
56+
body, err := io.ReadAll(r.Body)
57+
58+
if err != nil {
59+
utils.HandleError(w, err)
60+
return
61+
}
62+
63+
if err := json.Unmarshal(body, &requestBody); err != nil {
64+
utils.HandleError(w, err)
65+
return
66+
}
67+
68+
w.Header().Set("Content-Type", "application/json")
69+
w.WriteHeader(http.StatusOK)
70+
71+
responseBody := ResponseBody{
72+
Status: "OK",
73+
}
74+
75+
if err := json.NewEncoder(w).Encode(responseBody); err != nil {
76+
utils.HandleError(w, err)
77+
return
78+
}
79+
}
80+
81+
func HandleHead(w http.ResponseWriter, r *http.Request) {
82+
type RequestBody struct {
83+
ID string `json:"id"`
84+
}
85+
86+
var requestBody RequestBody
87+
88+
body, err := io.ReadAll(r.Body)
89+
90+
if err != nil {
91+
utils.HandleError(w, err)
92+
return
93+
}
94+
95+
if err := json.Unmarshal(body, &requestBody); err != nil {
96+
utils.HandleError(w, err)
97+
return
98+
}
99+
100+
w.Header().Set("Content-Type", "application/json")
101+
w.WriteHeader(http.StatusOK)
102+
}
103+
104+
func HandleOptions(w http.ResponseWriter, r *http.Request) {
105+
w.Header().Set("Allow", "OPTIONS")
106+
w.WriteHeader(http.StatusOK)
107+
}
108+
109+
func HandlePatch(w http.ResponseWriter, r *http.Request) {
110+
type RequestBody struct {
111+
ID string `json:"id"`
112+
}
113+
type ResponseBody struct {
114+
Status string `json:"status"`
115+
}
116+
117+
var requestBody RequestBody
118+
119+
body, err := io.ReadAll(r.Body)
120+
121+
if err != nil {
122+
utils.HandleError(w, err)
123+
return
124+
}
125+
126+
if err := json.Unmarshal(body, &requestBody); err != nil {
127+
utils.HandleError(w, err)
128+
return
129+
}
130+
131+
w.Header().Set("Content-Type", "application/json")
132+
w.WriteHeader(http.StatusOK)
133+
134+
responseBody := ResponseBody{
135+
Status: "OK",
136+
}
137+
138+
if err := json.NewEncoder(w).Encode(responseBody); err != nil {
139+
utils.HandleError(w, err)
140+
return
141+
}
142+
}
143+
144+
func HandlePost(w http.ResponseWriter, r *http.Request) {
145+
type RequestBody struct {
146+
ID string `json:"id"`
147+
}
148+
type ResponseBody struct {
149+
Status string `json:"status"`
150+
}
151+
152+
var requestBody RequestBody
153+
154+
body, err := io.ReadAll(r.Body)
155+
156+
if err != nil {
157+
utils.HandleError(w, err)
158+
return
159+
}
160+
161+
if err := json.Unmarshal(body, &requestBody); err != nil {
162+
utils.HandleError(w, err)
163+
return
164+
}
165+
166+
w.Header().Set("Content-Type", "application/json")
167+
w.WriteHeader(http.StatusOK)
168+
169+
responseBody := ResponseBody{
170+
Status: "OK",
171+
}
172+
173+
if err := json.NewEncoder(w).Encode(responseBody); err != nil {
174+
utils.HandleError(w, err)
175+
return
176+
}
177+
}
178+
179+
func HandlePut(w http.ResponseWriter, r *http.Request) {
180+
type RequestBody struct {
181+
ID string `json:"id"`
182+
}
183+
type ResponseBody struct {
184+
Status string `json:"status"`
185+
}
186+
187+
var requestBody RequestBody
188+
189+
body, err := io.ReadAll(r.Body)
190+
191+
if err != nil {
192+
utils.HandleError(w, err)
193+
return
194+
}
195+
196+
if err := json.Unmarshal(body, &requestBody); err != nil {
197+
utils.HandleError(w, err)
198+
return
199+
}
200+
201+
w.Header().Set("Content-Type", "application/json")
202+
w.WriteHeader(http.StatusOK)
203+
204+
responseBody := ResponseBody{
205+
Status: "OK",
206+
}
207+
208+
if err := json.NewEncoder(w).Encode(responseBody); err != nil {
209+
utils.HandleError(w, err)
210+
return
211+
}
212+
}
213+
214+
func HandleTrace(w http.ResponseWriter, r *http.Request) {
215+
w.Header().Set("Content-Type", "message/http")
216+
w.WriteHeader(http.StatusOK)
217+
_, _ = w.Write([]byte("TRACE /method/trace HTTP/1.1\r\nHost: example.com\r\n"))
218+
}

0 commit comments

Comments
 (0)