Skip to content

Commit d2e509f

Browse files
feat: add support for read/write only properties tests
1 parent 561b8c9 commit d2e509f

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

cmd/server/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import (
44
"log"
55
"net/http"
66

7+
"github.com/speakeasy-api/speakeasy-api-test-service/internal/acceptHeaders"
78
"github.com/speakeasy-api/speakeasy-api-test-service/internal/errors"
89
"github.com/speakeasy-api/speakeasy-api-test-service/internal/pagination"
10+
"github.com/speakeasy-api/speakeasy-api-test-service/internal/readonlywriteonly"
911
"github.com/speakeasy-api/speakeasy-api-test-service/internal/responseHeaders"
1012
"github.com/speakeasy-api/speakeasy-api-test-service/internal/retries"
1113

1214
"github.com/gorilla/mux"
1315
"github.com/speakeasy-api/speakeasy-api-test-service/internal/auth"
1416
"github.com/speakeasy-api/speakeasy-api-test-service/internal/requestbody"
15-
"github.com/speakeasy-api/speakeasy-api-test-service/internal/acceptHeaders"
1617
)
1718

1819
func main() {
@@ -29,6 +30,8 @@ func main() {
2930
r.HandleFunc("/retries", retries.HandleRetries).Methods(http.MethodGet)
3031
r.HandleFunc("/errors/{status_code}", errors.HandleErrors).Methods(http.MethodGet)
3132
r.HandleFunc("/optional", acceptHeaders.HandleAcceptHeaderMultiplexing).Methods(http.MethodGet)
33+
r.HandleFunc("/readonlyorwriteonly", readonlywriteonly.HandleReadOrWrite).Methods(http.MethodPost)
34+
r.HandleFunc("/readonlyandwriteonly", readonlywriteonly.HandleReadAndWrite).Methods(http.MethodPost)
3235

3336
log.Println("Listening on :8080")
3437
if err := http.ListenAndServe(":8080", r); err != nil {

internal/readonlywriteonly/service.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package readonlywriteonly
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+
type BasicObject struct {
12+
String string `json:"string"`
13+
Bool bool `json:"bool"`
14+
Num float64 `json:"num"`
15+
}
16+
17+
type InputObject struct {
18+
Num1 int64 `json:"num1"`
19+
Num2 int64 `json:"num2"`
20+
Num3 int64 `json:"num3"`
21+
}
22+
23+
type OutputObject struct {
24+
Num3 int64 `json:"num3"`
25+
Sum int64 `json:"sum"`
26+
}
27+
28+
func HandleReadOrWrite(w http.ResponseWriter, r *http.Request) {
29+
body, err := io.ReadAll(r.Body)
30+
if err != nil {
31+
utils.HandleError(w, err)
32+
return
33+
}
34+
35+
var req BasicObject
36+
if err := json.Unmarshal(body, &req); err != nil {
37+
utils.HandleError(w, err)
38+
return
39+
}
40+
41+
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
42+
43+
if err := json.NewEncoder(w).Encode(BasicObject{
44+
String: "hello",
45+
Bool: true,
46+
Num: 1.0,
47+
}); err != nil {
48+
utils.HandleError(w, err)
49+
}
50+
}
51+
52+
func HandleReadAndWrite(w http.ResponseWriter, r *http.Request) {
53+
body, err := io.ReadAll(r.Body)
54+
if err != nil {
55+
utils.HandleError(w, err)
56+
return
57+
}
58+
59+
var req InputObject
60+
if err := json.Unmarshal(body, &req); err != nil {
61+
utils.HandleError(w, err)
62+
return
63+
}
64+
65+
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
66+
67+
if err := json.NewEncoder(w).Encode(OutputObject{
68+
Num3: req.Num3,
69+
Sum: req.Num1 + req.Num2 + req.Num3,
70+
}); err != nil {
71+
utils.HandleError(w, err)
72+
}
73+
}

0 commit comments

Comments
 (0)