Skip to content

Commit 9ee38ff

Browse files
authored
feat: support to validate the response via JSON schema (#38)
1 parent 2f93d1d commit 9ee38ff

File tree

10 files changed

+256
-130
lines changed

10 files changed

+256
-130
lines changed

.github/workflows/release.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
id: meta
4949
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
5050
with:
51-
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
51+
images: ${{ env.REGISTRY }}/linuxsuren/api-testing
5252
- name: Build and push Docker image
5353
id: build-and-push
5454
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
@@ -72,7 +72,7 @@ jobs:
7272
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
7373
with:
7474
registry: ${{ env.REGISTRY_DOCKERHUB }}
75-
username: ${{ github.actor }}
75+
username: linuxsuren
7676
password: ${{ secrets.DOCKER_HUB_PUBLISH_SECRETS }}
7777
- name: Extract Docker metadata
7878
id: meta

README.md

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,72 @@
1-
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/3f16717cd6f841118006f12c346e9341)](https://www.codacy.com/gh/LinuxSuRen/api-testing/dashboard?utm_source=github.com&utm_medium=referral&utm_content=LinuxSuRen/api-testing&utm_campaign=Badge_Grade)
2-
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/3f16717cd6f841118006f12c346e9341)](https://www.codacy.com/gh/LinuxSuRen/api-testing/dashboard?utm_source=github.com&utm_medium=referral&utm_content=LinuxSuRen/api-testing&utm_campaign=Badge_Coverage)
1+
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/3f16717cd6f841118006f12c346e9341)](https://www.codacy.com/gh/LinuxSuRen/api-testing/dashboard?utm_source=github.com\&utm_medium=referral\&utm_content=LinuxSuRen/api-testing\&utm_campaign=Badge_Grade)
2+
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/3f16717cd6f841118006f12c346e9341)](https://www.codacy.com/gh/LinuxSuRen/api-testing/dashboard?utm_source=github.com\&utm_medium=referral\&utm_content=LinuxSuRen/api-testing\&utm_campaign=Badge_Coverage)
33
![GitHub All Releases](https://img.shields.io/github/downloads/linuxsuren/api-testing/total)
44

55
This is a API testing tool.
66

77
## Feature
8-
* Response Body fields equation check
9-
* Response Body [eval](https://expr.medv.io/)
10-
* Output reference between TestCase
11-
* Run in server mode, and provide the gRPC endpoint
12-
* [VS Code extension](https://github.com/LinuxSuRen/vscode-api-testing) support
8+
9+
* Response Body fields equation check
10+
* Response Body [eval](https://expr.medv.io/)
11+
* Validate the response body with [JSON schema](https://json-schema.org/)
12+
* Output reference between TestCase
13+
* Run in server mode, and provide the gRPC endpoint
14+
* [VS Code extension](https://github.com/LinuxSuRen/vscode-api-testing) support
15+
16+
## Get started
17+
18+
Install it via [hd](https://github.com/LinuxSuRen/http-downloader) or download from [releases](https://github.com/LinuxSuRen/api-testing/releases):
19+
20+
```shell
21+
hd install atest
22+
```
23+
24+
see the following usage:
25+
26+
```shell
27+
API testing tool
28+
29+
Usage:
30+
atest [command]
31+
32+
Available Commands:
33+
completion Generate the autocompletion script for the specified shell
34+
help Help about any command
35+
json Print the JSON schema of the test suites struct
36+
run Run the test suite
37+
sample Generate a sample test case YAML file
38+
server Run as a server mode
39+
40+
Flags:
41+
-h, --help help for atest
42+
-v, --version version for atest
43+
44+
Use "atest [command] --help" for more information about a command.
45+
```
46+
47+
below is an example of the usage, and you could see the report as well:
48+
49+
`atest run -p sample/testsuite-gitlab.yaml --duration 1m --thread 3 --report m`
50+
51+
| API | Average | Max | Min | Count | Error |
52+
|---|---|---|---|---|---|
53+
| GET https://gitlab.com/api/v4/projects | 1.152777167s | 2.108680194s | 814.928496ms | 99 | 0 |
54+
| GET https://gitlab.com/api/v4/projects/45088772 | 840.761064ms | 1.487285371s | 492.583066ms | 10 | 0 |
55+
consume: 1m2.153686448s
1356
1457
## Template
58+
1559
The following fields are templated with [sprig](http://masterminds.github.io/sprig/):
1660
17-
* API
18-
* Request Body
61+
* API
62+
* Request Body
63+
* Request Header
1964
2065
## TODO
21-
* Reduce the size of context
22-
* Support customized context
66+
67+
* Reduce the size of context
68+
* Support customized context
2369
2470
## Limit
25-
* Only support to parse the response body when it's a map or array
71+
72+
* Only support to parse the response body when it's a map or array

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ require (
3535
github.com/shopspring/decimal v1.2.0 // indirect
3636
github.com/spf13/cast v1.3.1 // indirect
3737
github.com/spf13/pflag v1.0.5 // indirect
38+
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
39+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
40+
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
3841
golang.org/x/crypto v0.3.0 // indirect
3942
golang.org/x/net v0.8.0 // indirect
4043
golang.org/x/sys v0.6.0 // indirect

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
602602
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
603603
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
604604
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
605+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
605606
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
606607
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
607608
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
@@ -612,6 +613,12 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
612613
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
613614
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
614615
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
616+
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
617+
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
618+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
619+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
620+
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
621+
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
615622
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
616623
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
617624
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

pkg/runner/simple.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/linuxsuren/api-testing/pkg/exec"
2121
"github.com/linuxsuren/api-testing/pkg/testing"
2222
unstructured "github.com/linuxsuren/unstructured/pkg"
23+
"github.com/xeipuuv/gojsonschema"
2324
)
2425

2526
// LevelWriter represents a writer with level
@@ -337,6 +338,8 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
337338
break
338339
}
339340
}
341+
342+
err = jsonSchemaValidation(testcase.Expect.Schema, responseBodyData)
340343
return
341344
}
342345

@@ -402,3 +405,18 @@ func expectString(name, expect, actual string) (err error) {
402405
}
403406
return
404407
}
408+
409+
func jsonSchemaValidation(schema string, body []byte) (err error) {
410+
if schema == "" {
411+
return
412+
}
413+
414+
schemaLoader := gojsonschema.NewStringLoader(schema)
415+
jsonLoader := gojsonschema.NewBytesLoader(body)
416+
417+
var result *gojsonschema.Result
418+
if result, err = gojsonschema.Validate(schemaLoader, jsonLoader); err == nil && !result.Valid() {
419+
err = fmt.Errorf("JSON schema validation failed: %v", result.Errors())
420+
}
421+
return
422+
}

0 commit comments

Comments
 (0)