Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Adding new YAML Endpoint #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"log"
"net/http"

"gopkg.in/yaml.v2"
)

type User struct {
Expand Down Expand Up @@ -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))
}
8 changes: 8 additions & 0 deletions test/integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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!"