Skip to content

Commit

Permalink
Using Sprintf and route fix
Browse files Browse the repository at this point in the history
  • Loading branch information
henrod committed Mar 3, 2017
1 parent 8ee6476 commit 23e363f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 64 deletions.
2 changes: 1 addition & 1 deletion api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (a *App) getRouter() *mux.Router {
NewValidationMiddleware(func() interface{} { return &models.Game{} }),
).ServeHTTP).Methods("PUT").Name("game")

r.Handle("/offer-templates", Chain(
r.Handle("/templates", Chain(
&OfferTemplateHandler{App: a, Method: "list"},
&NewRelicMiddleware{App: a},
&LoggingMiddleware{App: a},
Expand Down
15 changes: 8 additions & 7 deletions api/game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package api_test

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"

Expand All @@ -34,7 +35,7 @@ var _ = Describe("Game Handler", func() {
"Name": "Game Awesome Name",
"BundleID": "com.topfreegames.example",
})
request, _ := http.NewRequest("PUT", "/games/"+id, gameReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/games/%s", id), gameReader)

app.Router.ServeHTTP(recorder, request)

Expand All @@ -50,7 +51,7 @@ var _ = Describe("Game Handler", func() {
gameReader := JSONFor(JSON{
"Name": "Game Awesome Name",
})
request, _ := http.NewRequest("PUT", "/games/"+id, gameReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/games/%s", id), gameReader)

app.Router.ServeHTTP(recorder, request)

Expand All @@ -74,7 +75,7 @@ var _ = Describe("Game Handler", func() {
"Name": reallyBigName,
"BundleID": "com.topfreegames.example",
})
request, _ := http.NewRequest("PUT", "/games/"+id, gameReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/games/%s", id), gameReader)

app.Router.ServeHTTP(recorder, request)

Expand All @@ -98,7 +99,7 @@ var _ = Describe("Game Handler", func() {
"Name": uuid.NewV4().String(),
"BundleID": reallyBigName,
})
request, _ := http.NewRequest("PUT", "/games/"+id, gameReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/games/%s", id), gameReader)

app.Router.ServeHTTP(recorder, request)

Expand All @@ -120,7 +121,7 @@ var _ = Describe("Game Handler", func() {
"Name": name,
"BundleID": bundleID,
})
request, _ := http.NewRequest("PUT", "/games/"+id, gameReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/games/%s", id), gameReader)

app.Router.ServeHTTP(recorder, request)

Expand All @@ -142,7 +143,7 @@ var _ = Describe("Game Handler", func() {
"Name": name,
"BundleID": bundleID,
})
request, _ := http.NewRequest("PUT", "/games/"+id, gameReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/games/%s", id), gameReader)

app.Router.ServeHTTP(recorder, request)

Expand Down Expand Up @@ -180,7 +181,7 @@ var _ = Describe("Game Handler", func() {
Expect(err).NotTo(HaveOccurred())
app.DB = db
app.DB.(*runner.DB).DB.Close() // make DB connection unavailable
request, _ := http.NewRequest("PUT", "/games/"+id, gameReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/games/%s", id), gameReader)

app.Router.ServeHTTP(recorder, request)

Expand Down
52 changes: 20 additions & 32 deletions api/offer_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package api_test

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"

Expand Down Expand Up @@ -205,7 +206,7 @@ var _ = Describe("Offer Template Handler", func() {
"placement": placement,
})

request1, _ := http.NewRequest("PUT", "/templates/"+templateID+"/disable", offerTemplateReaderEnable)
request1, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/disable", templateID), offerTemplateReaderEnable)
request2, _ := http.NewRequest("POST", "/templates", offerTemplateReader)

app.Router.ServeHTTP(recorder, request1)
Expand All @@ -221,9 +222,8 @@ var _ = Describe("Offer Template Handler", func() {
It("should enable an enabled offer template", func() {
//Given
templateID := "dd21ec96-2890-4ba0-b8e2-40ea67196990"
url := "/templates/" + templateID + "/enable"
offerTemplateReader := JSONFor(JSON{})
request, _ := http.NewRequest("PUT", url, offerTemplateReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/enable", templateID), offerTemplateReader)

//When
app.Router.ServeHTTP(recorder, request)
Expand All @@ -236,9 +236,8 @@ var _ = Describe("Offer Template Handler", func() {
It("should enable a disabled offer template", func() {
//Given
templateID := "27b0370f-bd61-4346-a10d-50ec052ae125"
url := "/templates/" + templateID + "/enable"
offerTemplateReader := JSONFor(JSON{})
request, _ := http.NewRequest("PUT", url, offerTemplateReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/enable", templateID), offerTemplateReader)

//When
app.Router.ServeHTTP(recorder, request)
Expand All @@ -250,9 +249,8 @@ var _ = Describe("Offer Template Handler", func() {

It("returns status code of 500 if database is unavailable", func() {
templateID := "dd21ec96-2890-4ba0-b8e2-40ea67196990"
url := "/templates/" + templateID + "/enable"
offerTemplateReader := JSONFor(JSON{})
request, _ := http.NewRequest("PUT", url, offerTemplateReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/enable", templateID), offerTemplateReader)

oldDB := app.DB
db, err := GetTestDB()
Expand All @@ -274,9 +272,8 @@ var _ = Describe("Offer Template Handler", func() {
It("should return status code 404 if id doesn't exist", func() {
//Given
templateID := uuid.NewV4().String()
url := "/templates/" + templateID + "/enable"
offerTemplateReader := JSONFor(JSON{})
request, _ := http.NewRequest("PUT", url, offerTemplateReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/enable", templateID), offerTemplateReader)

//When
app.Router.ServeHTTP(recorder, request)
Expand All @@ -294,9 +291,8 @@ var _ = Describe("Offer Template Handler", func() {
It("should return status code 422 if invalid parameters", func() {
//Given
templateID := "not-uuid"
url := "/templates/" + templateID + "/enable"
offerTemplateReader := JSONFor(JSON{})
request, _ := http.NewRequest("PUT", url, offerTemplateReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/enable", templateID), offerTemplateReader)

//When
app.Router.ServeHTTP(recorder, request)
Expand All @@ -314,11 +310,10 @@ var _ = Describe("Offer Template Handler", func() {
It("should return error even if invalid id is passed in body", func() {
//Given
templateID := "dd21ec96-2890-4ba0-b8e2-40ea67196990"
url := "/templates/" + templateID + "/enable"
offerTemplateReader := JSONFor(JSON{
"id": "invalid-id",
})
request, _ := http.NewRequest("PUT", url, offerTemplateReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/enable", templateID), offerTemplateReader)

//When
app.Router.ServeHTTP(recorder, request)
Expand All @@ -338,9 +333,8 @@ var _ = Describe("Offer Template Handler", func() {
It("should disable an enabled offer template", func() {
//Given
templateID := "dd21ec96-2890-4ba0-b8e2-40ea67196990"
url := "/templates/" + templateID + "/disable"
offerTemplateReader := JSONFor(JSON{})
request, _ := http.NewRequest("PUT", url, offerTemplateReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/disable", templateID), offerTemplateReader)

//When
app.Router.ServeHTTP(recorder, request)
Expand All @@ -353,9 +347,8 @@ var _ = Describe("Offer Template Handler", func() {
It("should disable a disabled offer template", func() {
//Given
templateID := "27b0370f-bd61-4346-a10d-50ec052ae125"
url := "/templates/" + templateID + "/disable"
offerTemplateReader := JSONFor(JSON{})
request, _ := http.NewRequest("PUT", url, offerTemplateReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/disable", templateID), offerTemplateReader)

//When
app.Router.ServeHTTP(recorder, request)
Expand All @@ -368,11 +361,10 @@ var _ = Describe("Offer Template Handler", func() {
It("should use ID from URI even if a valid one is passed in body", func() {
//Given
templateID := "dd21ec96-2890-4ba0-b8e2-40ea67196990"
url := "/templates/" + templateID + "/disable"
offerTemplateReader := JSONFor(JSON{
"id": "aa65a3f2-7cf8-4d76-957f-0a23a1bbbd32",
})
request, _ := http.NewRequest("PUT", url, offerTemplateReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/disable", templateID), offerTemplateReader)

//When
app.Router.ServeHTTP(recorder, request)
Expand All @@ -384,9 +376,8 @@ var _ = Describe("Offer Template Handler", func() {

It("returns status code of 500 if database is unavailable", func() {
templateID := "dd21ec96-2890-4ba0-b8e2-40ea67196990"
url := "/templates/" + templateID + "/disable"
offerTemplateReader := JSONFor(JSON{})
request, _ := http.NewRequest("PUT", url, offerTemplateReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/disable", templateID), offerTemplateReader)

oldDB := app.DB
db, err := GetTestDB()
Expand All @@ -408,9 +399,8 @@ var _ = Describe("Offer Template Handler", func() {
It("should return status code 404 if id doesn't exist", func() {
//Given
templateID := uuid.NewV4().String()
url := "/templates/" + templateID + "/disable"
offerTemplateReader := JSONFor(JSON{})
request, _ := http.NewRequest("PUT", url, offerTemplateReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/disable", templateID), offerTemplateReader)

//When
app.Router.ServeHTTP(recorder, request)
Expand All @@ -428,9 +418,8 @@ var _ = Describe("Offer Template Handler", func() {
It("should return status code 422 if invalid parameters", func() {
//Given
templateID := "not-uuid"
url := "/templates/" + templateID + "/disable"
offerTemplateReader := JSONFor(JSON{})
request, _ := http.NewRequest("PUT", url, offerTemplateReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/disable", templateID), offerTemplateReader)

//When
app.Router.ServeHTTP(recorder, request)
Expand All @@ -448,11 +437,10 @@ var _ = Describe("Offer Template Handler", func() {
It("should return error even if invalid id is passed in body", func() {
//Given
templateID := "dd21ec96-2890-4ba0-b8e2-40ea67196990"
url := "/templates/" + templateID + "/disable"
offerTemplateReader := JSONFor(JSON{
"id": "invalid-id",
})
request, _ := http.NewRequest("PUT", url, offerTemplateReader)
request, _ := http.NewRequest("PUT", fmt.Sprintf("/templates/%s/disable", templateID), offerTemplateReader)

//When
app.Router.ServeHTTP(recorder, request)
Expand All @@ -468,9 +456,9 @@ var _ = Describe("Offer Template Handler", func() {
})
})

Describe("GET /offer-templates", func() {
Describe("GET /templates", func() {
It("should return status code of 200 and a list of offer templates", func() {
request, _ := http.NewRequest("GET", "/offer-templates?game-id=offers-game", nil)
request, _ := http.NewRequest("GET", "/templates?game-id=offers-game", nil)

app.Router.ServeHTTP(recorder, request)

Expand All @@ -495,15 +483,15 @@ var _ = Describe("Offer Template Handler", func() {
})

It("should return empty list if no offer templates", func() {
request, _ := http.NewRequest("GET", "/offer-templates?game-id=unexistent-game", nil)
request, _ := http.NewRequest("GET", "/templates?game-id=unexistent-game", nil)

app.Router.ServeHTTP(recorder, request)
Expect(recorder.Code).To(Equal(http.StatusOK))
Expect(recorder.Body.String()).To(Equal("[]"))
})

It("should return status code of 400 if game-id is not provided", func() {
request, _ := http.NewRequest("GET", "/offer-templates", nil)
request, _ := http.NewRequest("GET", "/templates", nil)

app.Router.ServeHTTP(recorder, request)

Expand All @@ -522,7 +510,7 @@ var _ = Describe("Offer Template Handler", func() {
Expect(err).NotTo(HaveOccurred())
app.DB = db
app.DB.(*runner.DB).DB.Close() // make DB connection unavailable
request, _ := http.NewRequest("GET", "/offer-templates?game-id=offers-game", nil)
request, _ := http.NewRequest("GET", "/templates?game-id=offers-game", nil)

app.Router.ServeHTTP(recorder, request)

Expand Down

0 comments on commit 23e363f

Please sign in to comment.