-
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.
- Loading branch information
Fergus Strange
committed
Jan 17, 2019
1 parent
b60abeb
commit a5cddc2
Showing
5 changed files
with
129 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/steinfletcher/api-test" | ||
"github.com/steinfletcher/api-test-jsonpath" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestGetUser_CookieMatching(t *testing.T) { | ||
apitest.New(). | ||
Handler(newApp().app). | ||
Get("/user/1234"). | ||
Expect(t). | ||
Cookies(apitest.NewCookie("TomsFavouriteDrink"). | ||
Value("Beer"). | ||
Path("/")). | ||
Status(http.StatusOK). | ||
End() | ||
} | ||
|
||
func TestGetUser_Success(t *testing.T) { | ||
apitest.New(). | ||
Handler(newApp().app). | ||
Get("/user/1234"). | ||
Expect(t). | ||
Body(`{"id": "1234", "name": "Andy"}`). | ||
Status(http.StatusOK). | ||
End() | ||
} | ||
|
||
func TestGetUser_Success_JSONPath(t *testing.T) { | ||
apitest.New(). | ||
Handler(newApp().app). | ||
Get("/user/1234"). | ||
Expect(t). | ||
Assert(jsonpath.Equal(`$.id`, "1234")). | ||
Status(http.StatusOK). | ||
End() | ||
} | ||
|
||
func TestGetUser_NotFound(t *testing.T) { | ||
apitest.New(). | ||
Handler(newApp().app). | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/labstack/echo" | ||
"net/http" | ||
) | ||
|
||
type User struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
type App struct { | ||
app *echo.Echo | ||
} | ||
|
||
func newApp() *App { | ||
app := echo.New() | ||
|
||
app.GET("/user/:id", getUser) | ||
|
||
return &App{ | ||
app: app, | ||
} | ||
} | ||
|
||
func (a *App) start() { | ||
a.app.Logger.Fatal(a.app.Start(":1323")) | ||
} | ||
|
||
func getUser(ctx echo.Context) error { | ||
ctx.SetCookie(&http.Cookie{ | ||
Name: "TomsFavouriteDrink", | ||
Value: "Beer", | ||
Path: "/", | ||
}) | ||
|
||
id := ctx.Param("id") | ||
|
||
if id == "1234" { | ||
return ctx.JSON(200, &User{ID: id, Name: "Andy"}) | ||
} else { | ||
return ctx.NoContent(404) | ||
} | ||
} | ||
|
||
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
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