Skip to content

Commit

Permalink
Merge pull request #13 from senseyeio/smotes/v2.1-compatibility
Browse files Browse the repository at this point in the history
smotes/v2.1 compatibility
  • Loading branch information
smotes authored Mar 19, 2021
2 parents 0e7cd30 + 3716b8c commit f626ebc
Show file tree
Hide file tree
Showing 13 changed files with 962 additions and 120 deletions.
13 changes: 7 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ services:
language: go

go:
- "1.10"
- "1.11"
- "1.12"
- "1.13"
- "1.14"
- "1.15"
- "1.16"

script: make unit && make integration

jobs:
include:
- stage: check
install: go get golang.org/x/lint/golint github.com/kisielk/errcheck
script: make lint && make errcheck
go: "1.12" # only run source code analysis tools on latest version of Go
install: go install golang.org/x/lint/golint@latest
script: make lint
go: "1.16" # only run source code analysis tools on latest version of Go
26 changes: 17 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
GOPACKAGES := $(shell go list ./...)
PACKAGES := $(shell go list ./... | grep -v mock)

.PHONY: default
default: fmt lint unit

.PHONY: errcheck
errcheck:
@errcheck -asserts -blank -ignore 'io:[cC]lose' $(GOPACKAGES)
default: fmt lint

.PHONY: fmt
## fmt: runs go fmt on source files
fmt:
@go fmt $(PACKAGES)

.PHONY: help
## help: prints this help message
help:
@echo "Usage: \n"
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'

.PHONY: integration
## integration: runs the integration tests
integration:
@sh ./scripts/integration_test.sh $(GOPACKAGES)
@go clean -testcache
@sh ./scripts/integration_test.sh $(PACKAGES)

.PHONY: lint
## lint: runs go lint on source files
lint:
@golint -set_exit_status $(GOPACKAGES)
@golint -set_exit_status -min_confidence=0.3 $(PACKAGES)

.PHONY: unit
## unit: runs the unit tests
unit:
@go test -cover -timeout=1s $(GOPACKAGES)
@go clean -testcache
@go test -cover -covermode=atomic -race -timeout=1s $(PACKAGES)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $ make integration

The integration tests expect Docker to be available on the host, using it to run a local mountebank container at
`localhost:2525`, with the additional ports 8080-8081 exposed for test imposters. Currently tested against a mountebank
v2.0.0 instance using the [andyrbell/mountebank](https://hub.docker.com/r/andyrbell/mountebank) image on DockerHub.
v2.1.2 instance using the [andyrbell/mountebank](https://hub.docker.com/r/andyrbell/mountebank) image on DockerHub.

## Contributing

Expand Down
137 changes: 136 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,141 @@ func (cli *Client) Imposter(ctx context.Context, port int, replay bool) (*Impost
return &imp, nil
}

// AddStub adds a new Stub without restarting its Imposter given the imposter's
// port and the new stub's index, or simply to the end of the array if index < 0.
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#add-stub
func (cli *Client) AddStub(ctx context.Context, port, index int, stub Stub) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d/stubs", port)

dto := map[string]interface{}{"stub": stub}
if index >= 0 {
dto["index"] = index
}
b, err := json.Marshal(dto)
if err != nil {
return nil, err
}

req, err := cli.restCli.NewRequest(ctx, http.MethodPost, p, bytes.NewReader(b), nil)
if err != nil {
return nil, err
}

resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}

var imp Imposter
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &imp); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return &imp, nil
}

// OverwriteStub overwrites an existing Stub without restarting its Imposter,
// where the stub index denotes the stub to be changed.
//
// See more information about this resouce at:
// http://www.mbtest.org/docs/api/overview#change-stub
func (cli *Client) OverwriteStub(ctx context.Context, port, index int, stub Stub) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d/stubs/%d", port, index)

b, err := json.Marshal(stub)
if err != nil {
return nil, err
}

req, err := cli.restCli.NewRequest(ctx, http.MethodPut, p, bytes.NewReader(b), nil)
if err != nil {
return nil, err
}

resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}

var imp Imposter
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &imp); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return &imp, nil
}

// OverwriteAllStubs overwrites all existing Stubs without restarting their Imposter.
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#change-stubs
func (cli *Client) OverwriteAllStubs(ctx context.Context, port int, stubs []Stub) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d/stubs", port)

b, err := json.Marshal(map[string]interface{}{
"stubs": stubs,
})
if err != nil {
return nil, err
}

req, err := cli.restCli.NewRequest(ctx, http.MethodPut, p, bytes.NewReader(b), nil)
if err != nil {
return nil, err
}

resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}

var imp Imposter
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &imp); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return &imp, nil
}

// RemoveStub removes a Stub without restarting its Imposter.
//
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#delete-stub
func (cli *Client) RemoveStub(ctx context.Context, port, index int) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d/stubs/%d", port, index)

req, err := cli.restCli.NewRequest(ctx, http.MethodDelete, p, http.NoBody, nil)
if err != nil {
return nil, err
}

resp, err := cli.restCli.Do(req)
if err != nil {
return nil, err
}

var imp Imposter
if resp.StatusCode == http.StatusOK {
if err := cli.restCli.DecodeResponseBody(resp.Body, &imp); err != nil {
return nil, err
}
} else {
return nil, cli.decodeError(resp.Body)
}
return &imp, nil
}

// Delete removes an Imposter configured on the given port and returns
// the deleted Imposter data, or an empty Imposter struct if one does not
// exist on the port.
Expand Down Expand Up @@ -169,7 +304,7 @@ func (cli *Client) Delete(ctx context.Context, port int, replay bool) (*Imposter
// See more information about this resource at:
// http://www.mbtest.org/docs/api/overview#delete-imposter-requests.
func (cli *Client) DeleteRequests(ctx context.Context, port int) (*Imposter, error) {
p := fmt.Sprintf("/imposters/%d/requests", port)
p := fmt.Sprintf("/imposters/%d/savedProxyResponses", port)

req, err := cli.restCli.NewRequest(ctx, http.MethodDelete, p, nil, nil)
if err != nil {
Expand Down
Loading

0 comments on commit f626ebc

Please sign in to comment.