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

[2.1] Add headers support #42

Merged
merged 16 commits into from
Oct 1, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Install dependencies
run: go get .
- name: Test with Go
run: go test -json > TestResults-${{ matrix.go-version }}.json
run: go test -json ./... 2>&1 | tee -a TestResults-${{ matrix.go-version }}.json
- name: Upload Go test results
uses: actions/upload-artifact@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ RUN mkdir /proto /stubs &&\
RUN cd /go/src/github.com/bavix/gripmock/protoc-gen-gripmock &&\
go install -v &&\
cd /go/src/github.com/bavix/gripmock/example/simple/client &&\
go get -u all &&\
go mod tidy &&\
cd /go/src/github.com/bavix/gripmock &&\
go install -v

Expand Down
76 changes: 75 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This service is a fork of the service [tokopedia/gripmock](https://github.com/to
- Updated all deprecated dependencies [tokopedia#64](https://github.com/tokopedia/gripmock/issues/64);
- Add yaml as json alternative for static stab's;
- Add endpoint for healthcheck (/api/health/liveness, /api/health/readiness);
- Add support headers [tokopedia#144](https://github.com/tokopedia/gripmock/issues/144);
- Add grpc error code [tokopedia#125](https://github.com/tokopedia/gripmock/issues/125);
- Added gzip encoding support for grpc server [tokopedia#134](https://github.com/tokopedia/gripmock/pull/134);
- Fixed issues with int64/uint64 [tokopedia#67](https://github.com/tokopedia/gripmock/pull/148);
Expand Down Expand Up @@ -74,13 +75,19 @@ Stub Format is JSON text format. It has a skeleton as follows:
{
"service":"<servicename>", // name of service defined in proto
"method":"<methodname>", // name of method that we want to mock
"headers":{ // Optional. headers matching rule. see Headers Matching Rule section below
// put rule here
},
"input":{ // input matching rule. see Input Matching Rule section below
// put rule here
},
"output":{ // output json if input were matched
"data":{
// put result fields here
},
"headers":{ // Optional
// put result headers here
},
"error":"<error message>", // Optional. if you want to return error instead.
"code":"<response code>" // Optional. Grpc response code. if code !=0 return error instead.
}
Expand Down Expand Up @@ -127,7 +134,7 @@ Stub will respond with the expected response only if the request matches any rul
So if you do a `curl -X POST -d '{"service":"Greeter","method":"SayHello","data":{"name":"gripmock"}}' localhost:4771/api/stubs/search` stub service will find a match from listed stubs stored there.

### Input Matching Rule
Input matching has 3 rules to match an input: **equals**,**contains** and **regex**
Input matching has 3 rules to match an input: **equals**,**contains** and **matches**
<br>
Nested fields are allowed for input matching too for all JSON data types. (`string`, `bool`, `array`, etc.)
<br>
Expand Down Expand Up @@ -191,6 +198,73 @@ Nested fields are allowed for input matching too for all JSON data types. (`stri
}
```

## Headers Matching
Stub will respond with the expected response only if the request matches any rule. Stub service will serve `/api/stubs/search` endpoint with format:
```json
{
"service":"<service name>",
"method":"<method name>",
"data":{
// input that suppose to match with stored stubs
}
}
```
So if you do a `curl -X POST -d '{"service":"Greeter","method":"SayHello","data":{"name":"gripmock"}}' localhost:4771/api/stubs/search` stub service will find a match from listed stubs stored there.

### Headers Matching Rule
Headers matching has 3 rules to match an input: **equals**,**contains** and **matches**
<br>
Headers can consist of a key and a value. If there are several values, then you need to list them separated by ";". Data type string.
<br>
**Gripmock** recursively goes over the fields and tries to match with given input.
<br>
**equals** will match the exact field name and value of input into expected stub. example stub JSON:
```json
{
.
.
"headers":{
"equals":{
"authorization": "mytoken",
"system": "ec071904-93bf-4ded-b49c-d06097ddc6d5"
}
}
.
.
}
```

**contains** will match input that has the value declared expected fields. example stub JSON:
```json
{
.
.
"headers":{
"contains":{
"field2":"hello"
}
}
.
.
}
```

**matches** using regex for matching fields expectation. example:

```json
{
.
.
"headers":{
"matches":{
"name":"^grip.*$"
}
}
.
.
}
```

---
Supported by

Expand Down
38 changes: 37 additions & 1 deletion api/openapi/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ openapi: 3.0.2
servers:
- url: /api
info:
version: 2.0.1
version: 2.1.0
title: GripMock API Schema
tags:
- name: stubs
Expand Down Expand Up @@ -198,6 +198,11 @@ components:
method:
type: string
example: SayHello
headers:
type: object
additionalProperties:
type: string
x-go-type-skip-optional-pointer: true
data:
type: object
x-go-type: interface{}
Expand All @@ -208,6 +213,11 @@ components:
- data
- error
properties:
headers:
type: object
additionalProperties:
type: string
x-go-type-skip-optional-pointer: true
data:
type: object
x-go-type: interface{}
Expand Down Expand Up @@ -239,6 +249,8 @@ components:
method:
type: string
example: SayHello
headers:
$ref: '#/components/schemas/StubHeaders'
input:
$ref: '#/components/schemas/StubInput'
output:
Expand All @@ -258,6 +270,25 @@ components:
type: object
additionalProperties: true
x-go-type-skip-optional-pointer: true
StubHeaders:
type: object
x-go-type-skip-optional-pointer: true
properties:
equals:
type: object
additionalProperties:
type: string
x-go-type-skip-optional-pointer: true
contains:
type: object
additionalProperties:
type: string
x-go-type-skip-optional-pointer: true
matches:
type: object
additionalProperties:
type: string
x-go-type-skip-optional-pointer: true
StubOutput:
type: object
required:
Expand All @@ -267,6 +298,11 @@ components:
data:
type: object
additionalProperties: true
headers:
type: object
additionalProperties:
type: string
x-go-type-skip-optional-pointer: true
error:
type: string
example: Message not found
Expand Down
4 changes: 4 additions & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
- [One service](proto-one-file)
- [N-Services in one](proto-multifiles)

- Matching Rule
- [Input](matching-rule-input)
- [Headers](matching-rule-headers)

- Static stubs
- [JSON](static-stubs-json)
- [YAML](static-stubs-yaml)
Expand Down
79 changes: 2 additions & 77 deletions docs/api-stubs-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,81 +69,6 @@ Response:
}
```

## Input Matching
Stub will respond with the expected response only if the request matches any rule. Stub service will serve `/api/stubs/search` endpoint with format:
```json
{
"service":"<service name>",
"method":"<method name>",
"data":{
// input that suppose to match with stored stubs
}
}
```
So if you do a `curl -X POST -d '{"service":"Greeter","method":"SayHello","data":{"name":"gripmock"}}' localhost:4771/api/stubs/search` stub service will find a match from listed stubs stored there.

### Input Matching Rule
Input matching has 3 rules to match an input: **equals**,**contains** and **regex**
<br>
Nested fields are allowed for input matching too for all JSON data types. (`string`, `bool`, `array`, etc.)
<br>
**Gripmock** recursively goes over the fields and tries to match with given input.
<br>
**equals** will match the exact field name and value of input into expected stub. example stub JSON:
```json
{
.
.
"input":{
"equals":{
"name":"gripmock",
"greetings": {
"english": "Hello World!",
"indonesian": "Halo Dunia!",
"turkish": "Merhaba Dünya!"
},
"ok": true,
"numbers": [4, 8, 15, 16, 23, 42]
"null": null
}
}
.
.
}
```

**contains** will match input that has the value declared expected fields. example stub JSON:
```json
{
.
.
"input":{
"contains":{
"field2":"hello",
"field4":{
"field5": "value5"
}
}
}
.
.
}
```

**matches** using regex for matching fields expectation. example:

```json
{
.
.
"input":{
"matches":{
"name":"^grip.*$",
"cities": ["Jakarta", "Istanbul", ".*grad$"]
}
}
.
.
}
```
[Input Matching](matching-rule-input.md ':include')

[Headers Matching](matching-rule-headers.md ':include')
66 changes: 66 additions & 0 deletions docs/matching-rule-headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## Headers Matching
Stub will respond with the expected response only if the request matches any rule. Stub service will serve `/api/stubs/search` endpoint with format:
```json
{
"service":"<service name>",
"method":"<method name>",
"data":{
// input that suppose to match with stored stubs
}
}
```
So if you do a `curl -X POST -d '{"service":"Greeter","method":"SayHello","data":{"name":"gripmock"}}' localhost:4771/api/stubs/search` stub service will find a match from listed stubs stored there.

### Headers Matching Rule
Headers matching has 3 rules to match an input: **equals**,**contains** and **matches**
<br>
Headers can consist of a key and a value. If there are several values, then you need to list them separated by ";". Data type string.
<br>
**Gripmock** recursively goes over the fields and tries to match with given input.
<br>
**equals** will match the exact field name and value of input into expected stub. example stub JSON:
```json
{
.
.
"headers":{
"equals":{
"authorization": "mytoken",
"system": "ec071904-93bf-4ded-b49c-d06097ddc6d5"
}
}
.
.
}
```

**contains** will match input that has the value declared expected fields. example stub JSON:
```json
{
.
.
"headers":{
"contains":{
"field2":"hello"
}
}
.
.
}
```

**matches** using regex for matching fields expectation. example:

```json
{
.
.
"headers":{
"matches":{
"name":"^grip.*$"
}
}
.
.
}
```
Loading