Skip to content

Commit

Permalink
add headers support
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Sep 26, 2023
1 parent de6171b commit d00013f
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 66 deletions.
36 changes: 36 additions & 0 deletions api/openapi/api.yaml
Original file line number Diff line number Diff line change
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
23 changes: 23 additions & 0 deletions example/simple/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/encoding/gzip"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

pb "github.com/bavix/gripmock/protogen/example/simple"
Expand Down Expand Up @@ -73,6 +74,28 @@ func main() {
}
log.Printf("Greeting: %s (return code %d)", r.Message, r.ReturnCode)

md := metadata.New(map[string]string{"Authorization": "Basic dXNlcjp1c2Vy"})
ctx = metadata.NewOutgoingContext(context.Background(), md)

var headers metadata.MD

name = "simple3"
r, err = c.SayHello(ctx, &pb.Request{Name: name}, grpc.Header(&headers))
if err != nil {
log.Fatalf("error from grpc: %v", err)
}
if r.ReturnCode != 0 {
log.Fatalf("grpc server returned code: %d, expected code: %d", r.ReturnCode, 0)
}
header := headers["result"]
if len(header) == 0 {
log.Fatal("the service did not return headers")
}
if header[0] != "ok" {
log.Fatal("the service returned an incorrect header")
}
log.Printf("Greeting: %s (return code %d)", r.Message, r.ReturnCode)

name = "simple3"
r, err = c.SayHello(context.Background(), &pb.Request{Name: name}, grpc.UseCompressor(gzip.Name))
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions example/simple/stub/simple3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@
data:
message: Hello Simple3
return_code: 3
- service: Gripmock
method: SayHello
headers:
contains:
authorization: Basic dXNlcjp1c2Vy # user:user
input:
equals:
name: simple3
output:
data:
message: Authorization OK
headers:
result: ok
1 change: 1 addition & 0 deletions internal/app/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type findStubPayload struct {
ID *uuid.UUID `json:"id,omitempty"`
Service string `json:"service"`
Method string `json:"method"`
Headers map[string]interface{} `json:"headers"`
Data map[string]interface{} `json:"data"`
}

Expand Down
79 changes: 63 additions & 16 deletions internal/app/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ import (
"github.com/bavix/gripmock/pkg/storage"
)

var ErrNotFound = errors.New("not found")

type matchFunc func(interface{}, interface{}) bool

type closeMatch struct {
rule string
expect map[string]interface{}
rule string
expect map[string]interface{}
headerRule string
headerExpect map[string]interface{}
}

func findStub(stubStorage *storage.StubStorage, stub *findStubPayload) (*storage.Output, error) {
Expand All @@ -41,36 +45,79 @@ func findStub(stubStorage *storage.StubStorage, stub *findStubPayload) (*storage
}

if stub.ID != nil {
stubStorage.MarkUsed(stubs[0].ID)

return &stubs[0].Output, nil
}

var closestMatch []closeMatch
for _, strange := range stubs {
if expect := strange.Input.Equals; expect != nil {
closestMatch = append(closestMatch, closeMatch{"equals", expect})
if equals(stub.Data, expect) {
return &strange.Output, nil
cmpData, cmpDataErr := inputCmp(strange.Input, stub.Data)
if cmpDataErr != nil {
if cmpData != nil {
closestMatch = append(closestMatch, *cmpData)
}
}

if expect := strange.Input.Contains; expect != nil {
closestMatch = append(closestMatch, closeMatch{"contains", expect})
if contains(strange.Input.Contains, stub.Data) {
return &strange.Output, nil
}
continue
}

if expect := strange.Input.Matches; expect != nil {
closestMatch = append(closestMatch, closeMatch{"matches", expect})
if matches(strange.Input.Matches, stub.Data) {
return &strange.Output, nil
if strange.CheckHeaders() {
if cmpHeaders, cmpHeadersErr := inputCmp(strange.Headers, stub.Headers); cmpHeadersErr != nil {
if cmpHeaders != nil {
closestMatch = append(closestMatch, closeMatch{
rule: cmpData.rule,
expect: cmpData.expect,
headerRule: cmpHeaders.rule,
headerExpect: cmpHeaders.expect,
})
}

continue
}
}

stubStorage.MarkUsed(strange.ID)

return &strange.Output, nil
}

return nil, stubNotFoundError(stub, closestMatch)
}

func inputCmp(input storage.Input, data map[string]interface{}) (*closeMatch, error) {
if expect := input.Equals; expect != nil {
closeMatchVal := closeMatch{rule: "equals", expect: expect}

if equals(input.Equals, data) {
return &closeMatchVal, nil
}

return &closeMatchVal, ErrNotFound
}

if expect := input.Contains; expect != nil {
closeMatchVal := closeMatch{rule: "contains", expect: expect}

if contains(input.Contains, data) {
return &closeMatchVal, nil
}

return &closeMatchVal, ErrNotFound
}

if expect := input.Matches; expect != nil {
closeMatchVal := closeMatch{rule: "matches", expect: expect}

if matches(input.Matches, data) {
return &closeMatchVal, nil
}

return &closeMatchVal, ErrNotFound
}

return nil, ErrNotFound
}

func stubNotFoundError(stub *findStubPayload, closestMatches []closeMatch) error {
template := fmt.Sprintf("Can't find stub \n\nService: %s \n\nMethod: %s \n\nInput\n\n", stub.Service, stub.Method)
expectString, err := json.MarshalIndent(stub.Data, "", "\t")
Expand Down
41 changes: 26 additions & 15 deletions internal/domain/rest/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 26 additions & 15 deletions pkg/sdk/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d00013f

Please sign in to comment.