Skip to content

Commit

Permalink
Merge pull request #74 from checkr/zz/add-base64-env-example
Browse files Browse the repository at this point in the history
Add headers rendering and example of basicauth
  • Loading branch information
zhouzhuojie authored Apr 28, 2020
2 parents c582703 + 7f5190e commit 19f7275
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ linters:
- golint
disable:
- maligned
- gocognit
presets:
- bugs
- complexity
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions demo_templates/http.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
68 changes: 46 additions & 22 deletions handle_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
}
Expand All @@ -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)
}
Expand All @@ -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
}

Expand All @@ -98,46 +109,59 @@ 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
}
}
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
}

0 comments on commit 19f7275

Please sign in to comment.