Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XML support #2

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 75 additions & 53 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ on:
branches:
- master
tags:
- '*.*.*' # semver, will override latest
- '*-preview' # preview, won't override latest
- "*.*.*" # semver, will override latest
- "*-preview" # preview, won't override latest
pull_request:
branches:
- master
Expand All @@ -22,7 +22,7 @@ jobs:

- uses: actions/setup-node@v3
with:
node-version: '${{ steps.nvm.outputs.NVMRC }}'
node-version: "${{ steps.nvm.outputs.NVMRC }}"
cache: yarn

- run: yarn install --frozen-lockfile
Expand All @@ -44,13 +44,13 @@ jobs:

- uses: actions/setup-node@v3
with:
node-version: '${{ steps.nvm.outputs.NVMRC }}'
node-version: "${{ steps.nvm.outputs.NVMRC }}"
cache: yarn

- name: Setup Go environment
uses: actions/setup-go@v4
with:
go-version: '^1.21.1'
go-version: "^1.21.1"

- run: yarn install --frozen-lockfile

Expand Down Expand Up @@ -80,64 +80,86 @@ jobs:

- uses: actions/setup-node@v3
with:
node-version: '${{ steps.nvm.outputs.NVMRC }}'
node-version: "${{ steps.nvm.outputs.NVMRC }}"
cache: yarn

- name: Setup Go environment
uses: actions/setup-go@v4
with:
go-version: '^1.21.1'
go-version: "^1.21.1"

- run: yarn install --frozen-lockfile

- name: Build
run: |
make VERSION=${{ steps.extract_ref.outputs.GIT_REF }} RELEASE=1 release
make VERSION=${{ steps.extract_ref.outputs.GIT_REF }} build-docker
make VERSION=${{ steps.extract_ref.outputs.GIT_REF }} start-docker

- if: startsWith(github.ref, 'refs/tags/')
uses: actions/upload-artifact@v3
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
name: smocker-bin
path: ./build/smocker.tar.gz

deploy:
needs: [lint, test, build]
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- id: extract_ref
run: echo ::set-output name=GIT_REF::$(echo ${GITHUB_REF##*/})
aws-region: us-east-1 # ECR Public can only be logged into from the us-east-1 region
role-to-assume: arn:aws:iam::202662887508:role/ecr-smocker
role-session-name: githubActions

- uses: actions/download-artifact@v3
- name: Login to Amazon ECR
id: login-ecr-public
uses: aws-actions/amazon-ecr-login@v2
with:
name: smocker-bin
path: ./build

- run: cd build && tar -xvf smocker.tar.gz
registry-type: public
mask-password: "true"

- name: Docker login
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
platforms: linux/amd64,linux/arm64

- name: Deploy on Docker registry
run: make VERSION=${{ steps.extract_ref.outputs.GIT_REF }} deploy-docker

- name: Deploy on GitHub releases
uses: softprops/action-gh-release@v1
with:
files: build/smocker.tar.gz
- name: Build
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
ECR_REGISTRY: ${{ steps.login-ecr-public.outputs.registry }}
ECR_REPOSITORY: smocker
IMAGE_TAG: ben
run: |
make VERSION=${{ steps.extract_ref.outputs.GIT_REF }} RELEASE=1 release

docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

# make VERSION=${{ steps.extract_ref.outputs.GIT_REF }} build-docker
# make VERSION=${{ steps.extract_ref.outputs.GIT_REF }} start-docker

# - if: startsWith(github.ref, 'refs/tags/')
# uses: actions/upload-artifact@v3
# with:
# name: smocker-bin
# path: ./build/smocker.tar.gz

# deploy:
# needs: [lint, test, build]
# if: startsWith(github.ref, 'refs/tags/')
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4

# - id: extract_ref
# run: echo ::set-output name=GIT_REF::$(echo ${GITHUB_REF##*/})

# - uses: actions/download-artifact@v3
# with:
# name: smocker-bin
# path: ./build

# - run: cd build && tar -xvf smocker.tar.gz

# - name: Docker login
# uses: docker/login-action@v3
# with:
# username: ${{ secrets.DOCKER_USERNAME }}
# password: ${{ secrets.DOCKER_PASSWORD }}

# - name: Set up QEMU
# uses: docker/setup-qemu-action@v3
# - name: Set up Docker Buildx
# uses: docker/setup-buildx-action@v3
# with:
# platforms: linux/amd64,linux/arm64

# - name: Deploy on Docker registry
# run: make VERSION=${{ steps.extract_ref.outputs.GIT_REF }} deploy-docker

# - name: Deploy on GitHub releases
# uses: softprops/action-gh-release@v1
# with:
# files: build/smocker.tar.gz
# env:
# GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
APPNAME=$(shell basename $(shell go list))
VERSION?=snapshot
VERSION?=ben
COMMIT=$(shell git rev-parse --verify HEAD)
DATE?=$(shell date +%FT%T%z)
RELEASE?=0
Expand Down
17 changes: 4 additions & 13 deletions docs/.vuepress/public/smocker.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
"$ref": "#/definitions/proxy"
}
},
"required": [
"request"
]
"required": ["request"]
},
"request": {
"type": "object",
Expand Down Expand Up @@ -169,12 +167,7 @@
"properties": {
"engine": {
"type": "string",
"enum": [
"go_template",
"go_template_yaml",
"go_template_json",
"lua"
]
"enum": ["go_template", "go_template_yaml", "go_template_json", "lua"]
},
"script": {
"type": "string"
Expand Down Expand Up @@ -223,6 +216,7 @@
"ShouldEndWith",
"ShouldEqual",
"ShouldEqualJSON",
"ShouldEqualXML",
"ShouldStartWith",
"ShouldMatch",
"ShouldNotResemble",
Expand All @@ -238,10 +232,7 @@
"type": "string"
}
},
"required": [
"matcher",
"value"
]
"required": ["matcher", "value"]
},
"headers": {
"type": "object",
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ require (
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/mitchellh/reflectwalk v1.0.1 // indirect
github.com/namsral/flag v1.7.4-pre
github.com/sbabiv/xml2map v1.2.1
github.com/sirupsen/logrus v1.4.2
github.com/smartystreets/assertions v1.0.1
github.com/stretchr/objx v0.2.0
github.com/stretchr/testify v1.7.0
github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf
github.com/yuin/gluamapper v0.0.0-20150323120927-d836955830e7
github.com/yuin/gopher-lua v0.0.0-20191220021717-ab39c6098bdb
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 h1:7HZCaLC5+BZpm
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs=
Expand Down Expand Up @@ -64,6 +66,8 @@ github.com/namsral/flag v1.7.4-pre h1:b2ScHhoCUkbsq0d2C15Mv+VU8bl8hAXV8arnWiOHNZ
github.com/namsral/flag v1.7.4-pre/go.mod h1:OXldTctbM6SWH1K899kPZcf65KxJiD7MsceFUpB5yDo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sbabiv/xml2map v1.2.1 h1:1lT7t0hhUvXZCkdxqtq4n8/ZCnwLWGq4rDuDv5XOoFE=
github.com/sbabiv/xml2map v1.2.1/go.mod h1:2TPoAfcaM7+Sd4iriPvzyntb2mx7GY+kkQpB/GQa/eo=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
Expand Down
91 changes: 91 additions & 0 deletions server/types/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"fmt"
"net/http"
"net/url"
"reflect"
"regexp"
"strings"

"github.com/sbabiv/xml2map"
log "github.com/sirupsen/logrus"
"github.com/smartystreets/assertions"
"github.com/stretchr/objx"
Expand All @@ -25,6 +28,7 @@ var asserts = map[string]Assertion{
"ShouldEndWith": assertions.ShouldEndWith,
"ShouldEqual": assertions.ShouldEqual,
"ShouldEqualJSON": assertions.ShouldEqualJSON,
"ShouldEqualXML": ShouldEqualXML,
"ShouldStartWith": assertions.ShouldStartWith,
"ShouldBeEmpty": ShouldBeEmpty,
"ShouldMatch": ShouldMatch,
Expand Down Expand Up @@ -59,6 +63,93 @@ func ShouldMatch(value interface{}, patterns ...interface{}) string {
return ""
}

func ShouldEqualXML(actual interface{}, expected ...interface{}) string {
//Transform string into map
decoder := xml2map.NewDecoder(strings.NewReader(expected[0].(string)))
exp, err := decoder.Decode()
if err != nil {
panic(err)
}

decoder = xml2map.NewDecoder(strings.NewReader(actual.(string)))
act, err := decoder.Decode()
if err != nil {
return fmt.Sprintf("error decoding `%s`, err is: %s", actual.(string), err)
}

res := walk(
reflect.ValueOf(exp),
reflect.ValueOf(act),
)

if !res {
return "zob"
}

return ""
}

func walk(v1, v2 reflect.Value) bool {
fmt.Printf("Visiting %v\n", v1)
// Indirect through pointers and interfaces
for v1.Kind() == reflect.Ptr || v1.Kind() == reflect.Interface {
v1 = v1.Elem()
v2 = v2.Elem()
}

switch v1.Kind() {
case reflect.Array, reflect.Slice:
if v1.Kind() != v2.Kind() {
return false
}
if v1.Len() != v2.Len() {
return false
}

for i := 0; i < v1.Len(); i++ {
ret := walk(v1.Index(i), v2.Index(i))
if !ret {
return false
}
}
case reflect.Map:
if v1.Kind() != v2.Kind() {
return false
}
if v1.Len() != v2.Len() {
return false
}

for _, k := range v1.MapKeys() {
v := v2.MapIndex(k)
if !v.IsValid() {
return false
}

ret := walk(v1.MapIndex(k), v2.MapIndex(k))
if !ret {
return false
}
}
case reflect.String:
if v1.String() == "<<IGNORE>>" {
return true
}
if v1.Kind() != v2.Kind() {
return false
}
return v1.String() == v2.String()

default:
if v1.Kind() != v2.Kind() {
return false
}
return v1.String() == v2.String()
}

return true
}

func ShouldBeEmpty(value interface{}, patterns ...interface{}) string {
return assertions.ShouldBeEmpty(value)
}
Expand Down
Loading
Loading