From b7aa14f7ed6d72e4019c23ab77a3ea3218c4b7f1 Mon Sep 17 00:00:00 2001 From: zhuojie Date: Mon, 27 Apr 2020 15:58:27 -0700 Subject: [PATCH 1/2] Add headers rendering and example of basicauth --- .golangci.yml | 1 + demo_templates/http.yaml | 12 +++++++ handle_actions.go | 68 +++++++++++++++++++++++++++------------- 3 files changed, 59 insertions(+), 22 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index b75d3607..a95d9b6b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,6 +5,7 @@ linters: - golint disable: - maligned + - gocognit presets: - bugs - complexity diff --git a/demo_templates/http.yaml b/demo_templates/http.yaml index 5dcda2a1..16f4bce0 100644 --- a/demo_templates/http.yaml +++ b/demo_templates/http.yaml @@ -193,3 +193,15 @@ - reply_http: status_code: 200 body: '{{ .HTTPBody | jsonPath "foo" }}' + +- key: base64-basicauth-with-env + kind: Behavior + expect: + http: + method: POST + path: /base64-basicauth-with-env + actions: + - reply_http: + headers: + Authorization: '{{ printf "foobar:%s" (env "BASIC_AUTH_PASS") | b64enc }}' + status_code: 200 diff --git a/handle_actions.go b/handle_actions.go index 47b5cb00..6906a8c1 100644 --- a/handle_actions.go +++ b/handle_actions.go @@ -9,23 +9,23 @@ import ( "github.com/sirupsen/logrus" ) -func (ms MocksArray) DoActions(c Context) error { +func (ms MocksArray) DoActions(ctx Context) error { for _, m := range ms { - if err := m.DoActions(c); err != nil { + if err := m.DoActions(ctx); err != nil { return nil } } return nil } -func (m *Mock) DoActions(c Context) error { - c.Values = m.Values - if !c.MatchCondition(m.Expect.Condition) { +func (m *Mock) DoActions(ctx Context) error { + ctx.Values = m.Values + if !ctx.MatchCondition(m.Expect.Condition) { return nil } for _, actionDispatcher := range m.Actions { actualAction := getActualAction(actionDispatcher) - if err := actualAction.Perform(c); err != nil { + if err := actualAction.Perform(ctx); err != nil { logrus.WithFields(logrus.Fields{ "err": err, "action": fmt.Sprintf("%T", actualAction), @@ -35,13 +35,13 @@ func (m *Mock) DoActions(c Context) error { return nil } -func (a ActionSendHTTP) Perform(context Context) error { - bodyStr, err := context.Render(a.Body) +func (a ActionSendHTTP) Perform(ctx Context) error { + bodyStr, err := ctx.Render(a.Body) if err != nil { return err } - urlStr, err := context.Render(a.URL) + urlStr, err := ctx.Render(a.URL) if err != nil { return err } @@ -50,6 +50,11 @@ func (a ActionSendHTTP) Perform(context Context) error { SetDebug(true). CustomMethod(a.Method, urlStr) + a.Headers, err = renderHeaders(ctx, a.Headers) + if err != nil { + return err + } + for k, v := range a.Headers { request.Set(k, v) } @@ -61,19 +66,25 @@ func (a ActionSendHTTP) Perform(context Context) error { return nil } -func (a ActionReplyHTTP) Perform(context Context) error { - ec := context.HTTPContext +func (a ActionReplyHTTP) Perform(ctx Context) (err error) { + ec := ctx.HTTPContext contentType := echo.MIMEApplicationJSON // default to JSON if ct, ok := a.Headers[echo.HeaderContentType]; ok { contentType = ct } + + a.Headers, err = renderHeaders(ctx, a.Headers) + if err != nil { + return err + } + for k, v := range a.Headers { ec.Response().Header().Set(k, v) } - msg, err := context.Render(a.Body) + msg, err := ctx.Render(a.Body) if err != nil { - logrus.WithField("err", err).Error("failed to render template for http") + logrus.WithField("err", err).Error("failed to render template for http body") return err } @@ -98,9 +109,9 @@ func (a ActionReplyHTTP) Perform(context Context) error { return nil } -func (a ActionRedis) Perform(context Context) error { +func (a ActionRedis) Perform(ctx Context) error { for _, cmd := range a { - _, err := context.Render(cmd) + _, err := ctx.Render(cmd) if err != nil { return err } @@ -108,36 +119,49 @@ func (a ActionRedis) Perform(context Context) error { return nil } -func (a ActionSleep) Perform(context Context) error { +func (a ActionSleep) Perform(ctx Context) error { time.Sleep(a.Duration) return nil } -func (a ActionPublishKafka) Perform(context Context) error { +func (a ActionPublishKafka) Perform(ctx Context) error { msg := a.Payload - msg, err := context.Render(msg) + msg, err := ctx.Render(msg) if err != nil { logrus.WithField("err", err).Error("failed to render template for kafka payload") return err } - err = context.om.kafkaClient.sendMessage(a.Topic, []byte(msg)) + err = ctx.om.kafkaClient.sendMessage(a.Topic, []byte(msg)) if err != nil { logrus.WithField("err", err).Error("failed to publish to kafka") } return err } -func (a ActionPublishAMQP) Perform(context Context) error { - msg, err := context.Render(a.Payload) +func (a ActionPublishAMQP) Perform(ctx Context) error { + msg, err := ctx.Render(a.Payload) if err != nil { logrus.WithField("err", err).Error("failed to render template for amqp") return err } publishToAMQP( - context.om.AMQPURL, + ctx.om.AMQPURL, a.Exchange, a.RoutingKey, msg, ) return nil } + +func renderHeaders(ctx Context, headers map[string]string) (map[string]string, error) { + ret := make(map[string]string) + for k, v := range headers { + msg, err := ctx.Render(v) + if err != nil { + logrus.WithField("err", err).Error("failed to render template for http headers") + return nil, err + } + ret[k] = msg + } + return ret, nil +} From 7f5190e0500455a90b9fa12948a646f862b344fe Mon Sep 17 00:00:00 2001 From: zhuojie Date: Mon, 27 Apr 2020 16:02:29 -0700 Subject: [PATCH 2/2] Bump golang version --- .circleci/config.yml | 2 +- Dockerfile | 2 +- Makefile | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index fc7a8a84..b58b1da8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -2,7 +2,7 @@ version: 2 jobs: unittest: docker: - - image: circleci/golang:1.12 + - image: circleci/golang:1.14 working_directory: /go/src/github.com/checkr/openmock steps: - checkout diff --git a/Dockerfile b/Dockerfile index 13029ba8..b8e8799b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.12 as builder +FROM golang:1.14 as builder WORKDIR /go/src/github.com/checkr/openmock ADD . . RUN make build diff --git a/Makefile b/Makefile index 295717cb..e2c43d6f 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ run: build lint: ifndef GOLANGCILINT @GO111MODULE=off go get -u github.com/myitcv/gobin - @gobin github.com/golangci/golangci-lint/cmd/golangci-lint@v1.17.1 + @gobin github.com/golangci/golangci-lint/cmd/golangci-lint@v1.24.0 endif @golangci-lint run