Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mkeeler/consul-mock-api
Consul Mock HTTP API helpers
https://github.com/mkeeler/consul-mock-api
Last synced: 23 days ago
JSON representation
Consul Mock HTTP API helpers
- Host: GitHub
- URL: https://github.com/mkeeler/consul-mock-api
- Owner: mkeeler
- License: mpl-2.0
- Created: 2020-09-11T14:53:12.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-12-15T17:28:44.000Z (almost 2 years ago)
- Last Synced: 2024-10-04T17:56:22.838Z (about 1 month ago)
- Language: Go
- Size: 30.3 KB
- Stars: 8
- Watchers: 2
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# consul-mock-api
Consul Mock HTTP API helpers## Usage
```go
package somepkgimport (
"testing""github.com/hashicorp/consul/api"
mockconsul "github.com/mkeeler/consul-mock-api"
)func TestTokenList(t *testing.T) {
m := mockconsul.NewConsul(t)// http.Get will add both of the headers but we don't want to care about them.
m.SetFilteredHeaders([]string{
"Accept-Encoding",
"User-Agent",
})// set up an expectation for a GET /v1/acl/tokens
m.ACLTokenList(200, []*api.ACLTokenListEntry{
CreateIndex: 1,
ModifyIndex: 2,
AccessorID: "cd45b25a-07cf-4a60-ae71-2a227eaace8e",
Description: "fake token",
NodeIdentities: []*api.ACLNodeIdentity{
{
NodeName: "foo",
Datacenter: "dc1",
},
},
}).Once() // expect this call to be made exactly once
cfg := api.DefaultConfig()
cfg.Address = m.URL()
client, err := api.NewClient(cfg)
if err != nil {
t.Fatalf("error when creating a new HTTP client: %v", err)
}
tokens, _, err := client.ACL().TokenList(nil)
if err != nil {
t.Fatalf("error when retrieving token list: %v", err)
}
if len(tokens) != 1 {
t.Fatalf("wrong number of token returned - expected: 1, got: %d", len(tokens))
}
// no need to do anything if using Go 1.14 as the expectations will be checked
// and the test HTTP server will be closed automatically. If not using Go 1.14 then
// you should defer m.Close() after its creation
}
```