From 1b653b25b99275a662ed4ddfeca88b89127a7335 Mon Sep 17 00:00:00 2001 From: Rob Dimsdale-Zucker Date: Fri, 20 Jan 2023 13:05:10 -0500 Subject: [PATCH] Add WithProtocol to Serve matcher. --- matchers/serve.go | 11 ++++++++++- matchers/serve_test.go | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/matchers/serve.go b/matchers/serve.go index 2cf61cb..ff34968 100644 --- a/matchers/serve.go +++ b/matchers/serve.go @@ -21,6 +21,7 @@ func Serve(expected interface{}) *ServeMatcher { return &ServeMatcher{ expected: expected, client: http.DefaultClient, + protocol: "http", docker: occam.NewDocker(), } } @@ -29,6 +30,7 @@ type ServeMatcher struct { expected interface{} port int endpoint string + protocol string docker occam.Docker response string client *http.Client @@ -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 { @@ -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 diff --git a/matchers/serve_test.go b/matchers/serve_test.go index ccbda74..82ce0a9 100644 --- a/matchers/serve_test.go +++ b/matchers/serve_test.go @@ -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() {