Skip to content

Commit

Permalink
Add WithProtocol to Serve matcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
robdimsdale authored and ryanmoran committed Jan 20, 2023
1 parent f529de6 commit 1b653b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
11 changes: 10 additions & 1 deletion matchers/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func Serve(expected interface{}) *ServeMatcher {
return &ServeMatcher{
expected: expected,
client: http.DefaultClient,
protocol: "http",
docker: occam.NewDocker(),
}
}
Expand All @@ -29,6 +30,7 @@ type ServeMatcher struct {
expected interface{}
port int
endpoint string
protocol string
docker occam.Docker
response string
client *http.Client
Expand Down Expand Up @@ -69,6 +71,13 @@ func (sm *ServeMatcher) WithEndpoint(endpoint string) *ServeMatcher {
return sm
}

// WithProtocol sets the protocol of the request.
// For example, WithProtocol("https") will make an https request
func (sm *ServeMatcher) WithProtocol(protocol string) *ServeMatcher {
sm.protocol = protocol
return sm
}

// WithDocker sets the occam.Docker that the matcher will use to access
// the 'actual' container's metadata.
func (sm *ServeMatcher) WithDocker(docker occam.Docker) *ServeMatcher {
Expand Down Expand Up @@ -104,7 +113,7 @@ func (sm *ServeMatcher) Match(actual interface{}) (success bool, err error) {
return false, errors.New(message)
}

response, err := sm.client.Get(fmt.Sprintf("http://%s:%s%s", container.Host(), container.HostPort(port), sm.endpoint))
response, err := sm.client.Get(fmt.Sprintf("%s://%s:%s%s", sm.protocol, container.Host(), container.HostPort(port), sm.endpoint))

if err != nil {
return false, err
Expand Down
16 changes: 16 additions & 0 deletions matchers/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,22 @@ func testServe(t *testing.T, context spec.G, it spec.S) {
})
})

context("when given a protocol", func() {
it.Before(func() {
matcher = matcher.WithProtocol("https")
})

it("uses provided protocol", func() {
_, err := matcher.Match(occam.Container{
Ports: map[string]string{
"8080": port,
},
Env: map[string]string{"PORT": "8080"},
})
Expect(err).To(MatchError(ContainSubstring("server gave HTTP response to HTTPS client")))
})
})

context("failure cases", func() {
context("the port is not in the container port mapping", func() {
it.Before(func() {
Expand Down

0 comments on commit 1b653b2

Please sign in to comment.