-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* abstract out *testing.T through an interface * add in ginkgo test example Co-authored-by: kuo.fong <[email protected]>
- Loading branch information
1 parent
a809d7c
commit d79f37b
Showing
10 changed files
with
217 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package server | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
type user struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
type application struct { | ||
Router *mux.Router | ||
} | ||
|
||
func NewApp() *application { | ||
router := mux.NewRouter() | ||
router.HandleFunc("/user/{id}", getUser()).Methods("GET") | ||
return &application{Router: router} | ||
} | ||
|
||
func (a *application) start() { | ||
log.Fatal(http.ListenAndServe("localhost:8888", a.Router)) | ||
} | ||
|
||
func getUser() http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
id := mux.Vars(r)["id"] | ||
http.SetCookie(w, &http.Cookie{ | ||
Name: "TomsFavouriteDrink", | ||
Value: "Beer", | ||
Path: "/", | ||
}) | ||
|
||
if id == "1234" { | ||
user := &user{ID: id, Name: "Andy"} | ||
bytes, _ := json.Marshal(user) | ||
_, err := w.Write(bytes) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusNotFound) | ||
} | ||
} | ||
|
||
func main() { | ||
NewApp().start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package server_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestHandlers(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Server Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package server_test | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
. "github.com/onsi/ginkgo" | ||
"github.com/steinfletcher/apitest" | ||
jsonpath "github.com/steinfletcher/apitest-jsonpath" | ||
server "github.com/steinfletcher/apitest/examples/ginkgo" | ||
) | ||
|
||
var _ = Describe("Ginkgo/Server", func() { | ||
|
||
var ( | ||
t GinkgoTInterface | ||
router *mux.Router | ||
) | ||
|
||
BeforeEach(func() { | ||
t = GinkgoT() | ||
router = server.NewApp().Router | ||
}) | ||
|
||
Context("Successful CookieMatching", func() { | ||
It("cookies should be set correctly", func() { | ||
apitest.New(). | ||
Handler(router). | ||
Get("/user/1234"). | ||
Expect(t). | ||
Cookies(apitest.NewCookie("TomsFavouriteDrink"). | ||
Value("Beer"). | ||
Path("/")). | ||
Status(http.StatusOK). | ||
End() | ||
}) | ||
}) | ||
|
||
Context("Successful GetUser", func() { | ||
It("Get User body should return desired value", func() { | ||
apitest.New(). | ||
Handler(router). | ||
Get("/user/1234"). | ||
Expect(t). | ||
Body(`{"id": "1234", "name": "Andy"}`). | ||
Status(http.StatusOK). | ||
End() | ||
}) | ||
|
||
It("Get User jsonpath should return desired value", func() { | ||
apitest.New(). | ||
Handler(router). | ||
Get("/user/1234"). | ||
Expect(t). | ||
Assert(jsonpath.Equal(`$.id`, "1234")). | ||
Status(http.StatusOK). | ||
End() | ||
}) | ||
}) | ||
|
||
Context("Unsuccessful GetUser", func() { | ||
It("User not found error should be raised", func() { | ||
apitest.New(). | ||
Handler(router). | ||
Get("/user/1515"). | ||
Expect(t). | ||
Status(http.StatusNotFound). | ||
End() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.