Skip to content

Commit

Permalink
feat: matcher marshaling
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorsalgado committed Aug 6, 2023
1 parent e3a4cf4 commit e049438
Show file tree
Hide file tree
Showing 71 changed files with 884 additions and 164 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ linters:
# - testableexamples
# - testpackage
# - thelper
# - tparallel
- tparallel
- typecheck
# - unconvert
# - unparam
Expand Down
5 changes: 5 additions & 0 deletions coretype/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package coretype

type SelfDescribing interface {
Describe() any
}
2 changes: 1 addition & 1 deletion dzgrpc/server_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (in *Interceptors) StreamInterceptor(
wrappedMocks[i] = &GRPCStreamMock{v}
}

description := dzstd.Description{Buf: make([]string, 0, len(mocks))}
description := dzstd.Results{Buf: make([]string, 0, len(mocks))}
result := dzstd.FindMockForRequest(stream.Context(), wrappedMocks, &StreamValueSelectorIn{
RequestMessage: rawBody,
Info: info,
Expand Down
24 changes: 12 additions & 12 deletions dzgrpc/server_stream_mock_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func ServerStreamMethod(method string) *ServerStreamMockBuilder {
b.m.streamExpectations = append(
b.m.streamExpectations,
&dzstd.Expectation[*StreamValueSelectorIn]{
Target: describeTarget(targetMethod, method),
Matcher: matcher.Contain(method),
ValueSelector: streamSelectMethod,
Weight: 10,
TargetDescription: describeTarget(targetMethod, method),
Matcher: matcher.Contain(method),
ValueSelector: streamSelectMethod,
Weight: 10,
},
)

Expand All @@ -37,10 +37,10 @@ func (b *ServerStreamMockBuilder) Header(key string, m matcher.Matcher) *ServerS
b.m.streamExpectations = append(
b.m.streamExpectations,
&dzstd.Expectation[*StreamValueSelectorIn]{
Target: describeTarget(targetHeader, key),
Matcher: m,
ValueSelector: streamSelectHeader(key),
Weight: 3,
TargetDescription: describeTarget(targetHeader, key),
Matcher: m,
ValueSelector: streamSelectHeader(key),
Weight: 3,
},
)

Expand All @@ -51,10 +51,10 @@ func (b *ServerStreamMockBuilder) Field(path string, m matcher.Matcher) *ServerS
b.m.streamExpectations = append(
b.m.streamExpectations,
&dzstd.Expectation[*StreamValueSelectorIn]{
Target: describeTarget(targetBodyField, path),
Matcher: matcher.Field(path, m),
ValueSelector: streamSelectBody,
Weight: 3,
TargetDescription: describeTarget(targetBodyField, path),
Matcher: matcher.Field(path, m),
ValueSelector: streamSelectBody,
Weight: 3,
},
)
return b
Expand Down
2 changes: 1 addition & 1 deletion dzgrpc/unary.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (in *Interceptors) UnaryInterceptor(
wrappedMocks[i] = &GRPCUnaryMock{v}
}

description := dzstd.Description{Buf: make([]string, 0, len(mocks))}
description := dzstd.Results{Buf: make([]string, 0, len(mocks))}
result := dzstd.FindMockForRequest(ctx, wrappedMocks, &UnaryValueSelectorIn{
RequestMessage: rawBody,
Info: info,
Expand Down
24 changes: 12 additions & 12 deletions dzgrpc/unary_mock_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func UnaryMethod(method string) *UnaryMockBuilder {
b.m.unaryExpectations = append(
b.m.unaryExpectations,
&dzstd.Expectation[*UnaryValueSelectorIn]{
Target: describeTarget(targetMethod, method),
Matcher: matcher.Contain(method),
ValueSelector: unarySelectMethod,
Weight: 10,
TargetDescription: describeTarget(targetMethod, method),
Matcher: matcher.Contain(method),
ValueSelector: unarySelectMethod,
Weight: 10,
},
)

Expand All @@ -37,10 +37,10 @@ func (b *UnaryMockBuilder) Header(key string, m matcher.Matcher) *UnaryMockBuild
b.m.unaryExpectations = append(
b.m.unaryExpectations,
&dzstd.Expectation[*UnaryValueSelectorIn]{
Target: describeTarget(targetHeader, key),
Matcher: m,
ValueSelector: unarySelectHeader(key),
Weight: 3,
TargetDescription: describeTarget(targetHeader, key),
Matcher: m,
ValueSelector: unarySelectHeader(key),
Weight: 3,
},
)

Expand All @@ -51,10 +51,10 @@ func (b *UnaryMockBuilder) Field(path string, m matcher.Matcher) *UnaryMockBuild
b.m.unaryExpectations = append(
b.m.unaryExpectations,
&dzstd.Expectation[*UnaryValueSelectorIn]{
Target: describeTarget(targetBodyField, path),
Matcher: matcher.Field(path, m),
ValueSelector: unarySelectBody,
Weight: 3,
TargetDescription: describeTarget(targetBodyField, path),
Matcher: matcher.Field(path, m),
ValueSelector: unarySelectBody,
Weight: 3,
},
)
return b
Expand Down
23 changes: 20 additions & 3 deletions dzhttp/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dzhttp

import (
"bytes"
"compress/gzip"
"context"
"fmt"
"net/http"
Expand Down Expand Up @@ -39,7 +40,7 @@ func (h *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.lifecycle.OnRequest(reqValues)

mocks := h.app.storage.GetEligible()
description := dzstd.Description{Buf: make([]string, 0, len(mocks))}
description := dzstd.Results{Buf: make([]string, 0, len(mocks))}
result := dzstd.FindMockForRequest(r.Context(), mocks,
&HTTPValueSelectorInput{r, parsedURL, r.URL.Query(), r.Form, parsedBody}, &description)

Expand Down Expand Up @@ -118,7 +119,23 @@ func (h *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.lifecycle.OnWarning(reqValues, err)
}
} else {
w.Write(stub.Body)
buf := new(bytes.Buffer)
gz := gzipper.Get().(*gzip.Writer)
defer gzipper.Put(gz)

gz.Reset(buf)

_, err := gz.Write(stub.Body)
if err != nil {
// TODO: handle error
}

err = gz.Close()
if err != nil {
// TODO: handle error
}

buf.WriteTo(w)
}
}

Expand Down Expand Up @@ -181,7 +198,7 @@ func (h *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

func (h *mockHandler) onNoMatches(w http.ResponseWriter, r *RequestValues, result *dzstd.FindResult[*HTTPMock], desc *dzstd.Description) {
func (h *mockHandler) onNoMatches(w http.ResponseWriter, r *RequestValues, result *dzstd.FindResult[*HTTPMock], desc *dzstd.Results) {
defer h.lifecycle.OnNoMatch(r, result, desc)

buf := bytes.Buffer{}
Expand Down
6 changes: 3 additions & 3 deletions dzhttp/http_handler_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
type mockHTTPLifecycle interface {
OnRequest(*RequestValues)
OnMatch(*RequestValues, *Stub)
OnNoMatch(*RequestValues, *dzstd.FindResult[*HTTPMock], *dzstd.Description)
OnNoMatch(*RequestValues, *dzstd.FindResult[*HTTPMock], *dzstd.Results)
OnWarning(*RequestValues, error)
OnError(*RequestValues, error)
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func (h *builtInMockHTTPLifecycle) OnMatch(r *RequestValues, s *Stub) {
evt.Msgf("<--- REQUEST MATCHED %s %s", r.RawRequest.Method, r.URL.Path)
}

func (h *builtInMockHTTPLifecycle) OnNoMatch(r *RequestValues, fr *dzstd.FindResult[*HTTPMock], d *dzstd.Description) {
func (h *builtInMockHTTPLifecycle) OnNoMatch(r *RequestValues, fr *dzstd.FindResult[*HTTPMock], d *dzstd.Results) {
if h.app.config.LogLevel == LogLevelDisabled {
return
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func (h *builtInDescriptiveMockHTTPLifecycle) OnMatch(rv *RequestValues, s *Stub
func (h *builtInDescriptiveMockHTTPLifecycle) OnNoMatch(
reqValues *RequestValues,
result *dzstd.FindResult[*HTTPMock],
d *dzstd.Description,
d *dzstd.Results,
) {
if h.app.config.LogLevel == LogLevelDisabled {
return
Expand Down
Loading

0 comments on commit e049438

Please sign in to comment.