From 8cb37c296ce40e24d7671c3daf79871dd78110e8 Mon Sep 17 00:00:00 2001 From: Chris Corcoran Date: Fri, 30 Oct 2020 15:03:51 -0700 Subject: [PATCH] Adding yaml endpoint --- main.go | 12 ++++++++++++ test/integration-test.sh | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/main.go b/main.go index caa14d9..1feca7f 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,8 @@ import ( "encoding/json" "log" "net/http" + + "gopkg.in/yaml.v2" ) type User struct { @@ -32,5 +34,15 @@ func main() { enc.Encode(myUsers) }) + // Let's say someone added a new endpoint to return YAML instead of JSON... + // what could possibly go wrong? + http.HandleFunc("/users/yaml", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/x-yaml") + w.WriteHeader(200) + enc := yaml.NewEncoder(w) + defer enc.Close() + enc.Encode(myUsers) + }) + log.Fatal(http.ListenAndServe(":8080", nil)) } diff --git a/test/integration-test.sh b/test/integration-test.sh index 4f14075..49bf2c7 100755 --- a/test/integration-test.sh +++ b/test/integration-test.sh @@ -8,4 +8,12 @@ if [ $json_status_code != 200 ]; then exit 1 fi +# Test the YAML endpoint +yaml_status_code=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:8080/users/yaml) + +if [ $yaml_status_code != 200 ]; then + echo "JSON endpoint returned error code: $yaml_status_code" + exit 1 +fi + echo "All test passed successfully!"