Example - Gin
Gin is an excellent framework for building
RESTful APIs.
The following example is a simple Login UI (Consumer) that calls a
User Service (Provider) using JSON over HTTP.
The API currently exposes a single Login
endpoint at POST /users/login
, which
the Consumer uses to authenticate a User.
We test 3 scenarios, highlighting the use of Provider States:
- When the user "Billy" exists, and we perform a login, we expect an HTTP
200
- When the user "Billy" does not exists, and we perform a login, we expect an HTTP
404
- When the user "Billy" is unauthorized, and we perform a login, we expect an HTTP
403
Getting started
Before any of these tests can be run, ensure Pact Go is installed and run the
daemon in the background:
go get ./...
<path to>/pact-go daemon
Provider
The "Provider" is a real Go Kit Endpoint (following the Profile Service example),
exposing a single /users/login
API call:
cd provider
go test -v .
This will spin up the Provider API with extra routes added for the handling of
provider states, run the verification process and report back success/failure.
Running the Provider
The provider can be run as a standalone service:
go run cmd/usersvc/main.go
# 200
curl -v -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"username":"billy",
"password":"issilly"
}' "http://localhost:8080/users/login"
# 403
curl -v -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"username":"billy",
"password":"issilly"
}' "http://localhost:8080/users/login"
# 404
curl -v -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"username":"someoneelse",
"password":"issilly"
}' "http://localhost:8080/users/login"