Skip to content

Commit

Permalink
adds return values to the Mountebank Client
Browse files Browse the repository at this point in the history
  • Loading branch information
durmaze committed Feb 8, 2016
1 parent a91371d commit 138c40a
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 35 deletions.
3 changes: 0 additions & 3 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
- add icon
- update README
- Mountebank Client
- currently mountebank base uri is hard-coded, get it from config.
- use Hypermedia links
- shape up Mountebank client Api. currently only updates Mountebank, no return values.
- update test assertions properly. add count-check for the imposters.
- add get, getAll imposters
- add all predicates (erkan)
- add all response types and response behaviors (alper)
- switch to JsonPath
- add JsonPath support to Mountebank as predicate parameter - pull request
Expand Down
67 changes: 55 additions & 12 deletions mountebank/mountebank.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,76 @@
package mountebank

import (
"strconv"
"encoding/json"
"errors"
"net/http"
"strconv"

. "github.com/durmaze/gobank/builders"
"github.com/parnurzeal/gorequest"
)

type Client struct{
type Client struct {
mountebankUri string
impostersUri string
impostersUri string
}

func NewClient(mountebankUri string) *Client {
return &Client{
mountebankUri: mountebankUri,
impostersUri: mountebankUri + "/imposters",
impostersUri: mountebankUri + "/imposters",
}
}

func (c *Client) CreateImposter(imposter Imposter) {
gorequest.New().Post(c.impostersUri).Send(imposter).End()
func (c *Client) CreateImposter(imposter Imposter) (map[string]interface{}, error) {
resp, body, errs := gorequest.New().Post(c.impostersUri).Send(imposter).EndBytes()

if len(errs) > 0 {
return nil, errs[0]
}

if resp.StatusCode == http.StatusCreated {
var created map[string]interface{}
json.Unmarshal(body, &created)

return created, nil
}

return nil, errors.New("Cannot create the imposter")
}

func (c *Client) DeleteImposter(imposter Imposter) {
imposterUri := c.impostersUri + "/" + strconv.Itoa(imposter.Port)
func (c *Client) DeleteImposter(port int) (map[string]interface{}, error) {
imposterUri := c.impostersUri + "/" + strconv.Itoa(port)

resp, body, errs := gorequest.New().Delete(imposterUri).EndBytes()

if len(errs) > 0 {
return nil, errs[0]
}

if resp.StatusCode == http.StatusOK {
var deleted map[string]interface{}
json.Unmarshal(body, &deleted)

return deleted, nil
}

gorequest.New().Delete(imposterUri).End()
return nil, errors.New("Cannot delete the imposter")
}

func (c *Client) DeleteAllImposters() {
gorequest.New().Delete(c.impostersUri).End()
}
func (c *Client) DeleteAllImposters() (map[string]interface{}, error) {
resp, body, errs := gorequest.New().Delete(c.impostersUri).EndBytes()

if len(errs) > 0 {
return nil, errs[0]
}

if resp.StatusCode == http.StatusOK {
var allDeleted map[string]interface{}
json.Unmarshal(body, &allDeleted)

return allDeleted, nil
}

return nil, errors.New("Cannot delete all of the imposters")
}
103 changes: 83 additions & 20 deletions mountebank/mountebank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"net/http"
"strconv"
"sync"

"github.com/durmaze/gobank/builders"
"github.com/durmaze/gobank/mountebank"
Expand All @@ -20,48 +21,82 @@ var _ = Describe("Mountebank Client", func() {
Describe("When createImposter request is sent to Mountebank", func() {

var (
protocol = "http"
port = 4546
protocol = "http"
port = 4546
createdImposter map[string]interface{}
expectedImposter builders.Imposter
err error

once sync.Once
)

BeforeEach(func() {
okResponse := responses.Is().StatusCode(200).Body("{ \"greeting\": \"Hello GoBank\" }").Build()
once.Do(func() {
okResponse := responses.Is().StatusCode(200).Body("{ \"greeting\": \"Hello GoBank\" }").Build()

equals := predicates.Equals().Path("/test-path").Build()
contains := predicates.Contains().Header("Accept", "application/json").Build()
exists := predicates.Exists().Method(true).Query("q", false).Body(false).Build()
or := predicates.Or().Predicates(equals, contains, exists).Build()
equals := predicates.Equals().Path("/test-path").Build()
contains := predicates.Contains().Header("Accept", "application/json").Build()
exists := predicates.Exists().Method(true).Query("q", false).Body(false).Build()
or := predicates.Or().Predicates(equals, contains, exists).Build()

stub := builders.Stub().Responses(okResponse).Predicates(or).Build()
stub := builders.Stub().Responses(okResponse).Predicates(or).Build()

imposter := builders.NewImposterBuilder().Protocol(protocol).Port(port).Name("Greeting Imposter").Stubs(stub).Build()
expectedImposter = builders.NewImposterBuilder().Protocol(protocol).Port(port).Name("Greeting Imposter").Stubs(stub).Build()

client := mountebank.NewClient(MountebankUri)
client.CreateImposter(imposter)
client := mountebank.NewClient(MountebankUri)
createdImposter, err = client.CreateImposter(expectedImposter)
log.Println("ActualImposter: ", createdImposter)
})
})

It("should have the Imposter installed on Mountebank", func() {
imposterUri := MountebankUri + "/imposters/" + strconv.Itoa(port)
resp, body, _ := gorequest.New().Get(imposterUri).End()

log.Println(body)
log.Println("Imposter from Mountebank. Body: ", body)
Expect(resp.StatusCode).To(Equal(http.StatusOK))
})

It("should return the correct protocol", func() {
Expect(createdImposter["protocol"]).To(Equal(protocol))
})

It("should return the correct port", func() {
Expect(createdImposter["port"]).To(Equal(float64(port)))
})

It("should return the correct name", func() {
Expect(createdImposter["name"]).To(Equal("Greeting Imposter"))
})

It("should return one stub", func() {
Expect(createdImposter["stubs"]).To(HaveLen(1))
})

It("should not return an error", func() {
Expect(err).To(BeNil())
})
})

Describe("When deleteImposter request is sent to Mountebank", func() {

var (
protocol = "http"
port = 5000
protocol = "http"
port = 5000
deletedImposter map[string]interface{}
err error

once sync.Once
)

BeforeEach(func() {
imposter := builders.NewImposterBuilder().Protocol(protocol).Port(port).Build()
client := mountebank.NewClient(MountebankUri)
client.CreateImposter(imposter)
once.Do(func() {
imposter := builders.NewImposterBuilder().Protocol(protocol).Port(port).Build()
client := mountebank.NewClient(MountebankUri)
client.CreateImposter(imposter)

client.DeleteImposter(imposter)
deletedImposter, err = client.DeleteImposter(port)
})
})

It("should delete the installed Imposter at Mountebank", func() {
Expand All @@ -71,13 +106,38 @@ var _ = Describe("Mountebank Client", func() {
Expect(resp.StatusCode).To(Equal(http.StatusNotFound))
})

It("should return the correct protocol", func() {
Expect(deletedImposter["protocol"]).To(Equal(protocol))
})

It("should return the correct port", func() {
Expect(deletedImposter["port"]).To(Equal(float64(port)))
})

It("should not return an error", func() {
Expect(err).To(BeNil())
})
})

Describe("When deleteAllImposter request is sent to Mountebank", func() {
var (
protocol = "http"
err error

once sync.Once
)

BeforeEach(func() {
client := mountebank.NewClient(MountebankUri)
client.DeleteAllImposters()
once.Do(func() {
imposter1 := builders.NewImposterBuilder().Protocol(protocol).Build()
imposter2 := builders.NewImposterBuilder().Protocol(protocol).Build()

client := mountebank.NewClient(MountebankUri)
client.CreateImposter(imposter1)
client.CreateImposter(imposter2)

_, err = client.DeleteAllImposters()
})
})

It("should delete all the installed Imposters at Mountebank", func() {
Expand All @@ -87,6 +147,9 @@ var _ = Describe("Mountebank Client", func() {
Expect(resp.StatusCode).To(Equal(http.StatusOK))
})

It("should not return an error", func() {
Expect(err).To(BeNil())
})
})

})

0 comments on commit 138c40a

Please sign in to comment.