Skip to content

Add endpoint to retrieve all greetings #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
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
19 changes: 16 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"math/rand"
"net/http"
"os"

"github.com/gorilla/mux"
"github.com/rs/cors"
Expand All @@ -26,7 +25,7 @@ func main() {
err := json.Unmarshal(greetingsJson, &greetings)
if err != nil {
fmt.Printf("error loading greetings: %s\n", err)
os.Exit(1)
panic(err)
}
router := mux.NewRouter()

Expand Down Expand Up @@ -57,6 +56,20 @@ func main() {
}
}).Methods("GET")

router.HandleFunc("/all", func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("got /all request from %s\n", r.RemoteAddr)
w.Header().Set("Content-Type", "application/json")
jsonGreetings, err := json.Marshal(greetings)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write(jsonGreetings)
if err != nil {
panic(err)
}
}).Methods("GET")

c := cors.New(cors.Options{
AllowedOrigins: []string{
"http://greetings.kylepenfound.com",
Expand All @@ -70,7 +83,7 @@ func main() {
fmt.Printf("server closed\n")
} else if err != nil {
fmt.Printf("error starting server: %s\n", err)
os.Exit(1)
panic(err)
}
}

Expand Down
49 changes: 47 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"encoding/json"
"fmt"
"os"
"net/http"
"net/http/httptest"
"testing"

"github.com/gorilla/mux"
"gotest.tools/v3/assert"
)

Expand All @@ -14,7 +16,7 @@ func TestSelectGreeting(t *testing.T) {
err := json.Unmarshal(greetingsJson, &greetings)
if err != nil {
fmt.Printf("error loading greetings: %s\n", err)
os.Exit(1)
panic(err)
}

english := &Greeting{
Expand Down Expand Up @@ -49,3 +51,46 @@ func TestFormatResponse(t *testing.T) {
formatted := FormatResponse(g)
assert.Equal(t, "{\"greeting\":\"Hello, World!\"}", formatted)
}

func TestAllEndpoint(t *testing.T) {
var greetings []*Greeting
err := json.Unmarshal(greetingsJson, &greetings)
if err != nil {
fmt.Printf("error loading greetings: %s\n", err)
panic(err)
}

router := mux.NewRouter()
router.HandleFunc("/all", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
jsonGreetings, err := json.Marshal(greetings)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write(jsonGreetings)
if err != nil {
panic(err)
}
}).Methods("GET")

rec := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/all", nil)
if err != nil {
t.Fatal(err)
}

router.ServeHTTP(rec, req)

if rec.Code != http.StatusOK {
t.Fatalf("expected status OK; got %v", rec.Code)
}

var actualGreetings []*Greeting
err = json.Unmarshal(rec.Body.Bytes(), &actualGreetings)
if err != nil {
t.Fatal(err)
}

assert.DeepEqual(t, greetings, actualGreetings)
}