Skip to content

Commit 629e70a

Browse files
committed
replace xml with text/plain.
1 parent 83ad29e commit 629e70a

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

cmd/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func main() {
2828
r.HandleFunc("/pagination/cursor", pagination.HandleCursor).Methods(http.MethodGet, http.MethodPut)
2929
r.HandleFunc("/retries", retries.HandleRetries).Methods(http.MethodGet)
3030
r.HandleFunc("/errors/{status_code}", errors.HandleErrors).Methods(http.MethodGet)
31-
r.HanldeFunc("/optional", acceptHeaders.AcceptHeaders).Methods(http.MethodGet)
31+
r.HandleFunc("/optional", acceptHeaders.HandleAcceptHeaderMultiplexing).Methods(http.MethodGet)
3232

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

internal/acceptHeaders/service.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@ package acceptHeaders
22

33
import (
44
"encoding/json"
5-
"encoding/xml"
65
"net/http"
6+
"strings"
77

88
"github.com/speakeasy-api/speakeasy-api-test-service/internal/utils"
99
)
1010

11+
func headersContains(headers []string, toCheck string) bool {
12+
for _, a := range headers {
13+
if strings.Contains(a, toCheck) {
14+
return true
15+
}
16+
}
17+
return false
18+
}
19+
1120
func HandleAcceptHeaderMultiplexing(w http.ResponseWriter, r *http.Request) {
1221
var obj interface{}
13-
if r.Header["Accept"] == "application/json" {
14-
err := json.Unmarshal([]byte("{\"Obj1\":\"obj1\"}"), &obj)
22+
if headersContains(r.Header["Accept"], "application/json") {
23+
err := json.Unmarshal([]byte("{\"type\":\"obj1\", \"value\": \"JSON\"}"), &obj)
1524
if err != nil {
1625
utils.HandleError(w, err)
1726
return
@@ -22,18 +31,10 @@ func HandleAcceptHeaderMultiplexing(w http.ResponseWriter, r *http.Request) {
2231
if err := json.NewEncoder(w).Encode(obj); err != nil {
2332
utils.HandleError(w, err)
2433
}
25-
} else if r.Header["Accept"] == "application/xml" {
26-
err := json.Unmarshal([]byte("{\"Obj2\":\"obj2\"}"), &obj)
27-
if err != nil {
28-
utils.HandleError(w, err)
29-
return
30-
}
31-
32-
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
33-
34-
if err := xml.NewEncoder(w).Encode(obj); err != nil {
35-
utils.HandleError(w, err)
36-
}
34+
} else if headersContains(r.Header["Accept"], "text/plain") {
35+
36+
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
37+
w.Write([]byte("Success"))
3738
}
3839

3940
}

0 commit comments

Comments
 (0)