Skip to content

Commit

Permalink
Merge pull request #241 from pace/external-depenency
Browse files Browse the repository at this point in the history
External depenency
  • Loading branch information
threez committed Dec 15, 2020
2 parents 0ab657f + 851e1a0 commit 0190d91
Show file tree
Hide file tree
Showing 31 changed files with 474 additions and 80 deletions.
3 changes: 3 additions & 0 deletions backend/queue/rmq.go
Expand Up @@ -63,6 +63,9 @@ func NewQueue(name string, healthyLimit int) (rmq.Queue, error) {
return nil, err
}
queue, err := rmqConnection.OpenQueue(name)
if err != nil {
return nil, err
}
if _, ok := queueHealthLimits.Load(name); ok {
return queue, nil
}
Expand Down
6 changes: 4 additions & 2 deletions backend/queue/rmq_test.go
Expand Up @@ -16,15 +16,17 @@ func TestIntegrationHealthCheck(t *testing.T) {
ctx := log.WithContext(context.Background())
q1, err := queue.NewQueue("integrationTestTasks", 1)
assert.NoError(t, err)
q1.Publish("nothing here")
err = q1.Publish("nothing here")
assert.NoError(t, err)

check := &queue.HealthCheck{IgnoreInterval: true}
res := check.HealthCheck(ctx)
if res.State != "OK" {
t.Errorf("Expected health check to be OK for a non-full queue")
}

q1.Publish("nothing here either")
err = q1.Publish("nothing here either")
assert.NoError(t, err)

res = check.HealthCheck(ctx)
if res.State == "OK" {
Expand Down
5 changes: 4 additions & 1 deletion http/jsonapi/errors_test.go
Expand Up @@ -50,7 +50,10 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
var writer io.Writer = buffer

_ = MarshalErrors(writer, testRow.In)
json.Unmarshal(buffer.Bytes(), &output)
err := json.Unmarshal(buffer.Bytes(), &output)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(output, testRow.Out) {
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)
Expand Down
2 changes: 0 additions & 2 deletions http/jsonapi/generator/generate_helper.go
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/getkin/kin-openapi/openapi3"
)

const pkgRuntime = "github.com/pace/bricks/http/jsonapi/runtime"

func (g *Generator) addGoDoc(typeName, description string) {
if description != "" {
g.goSource.Comment(fmt.Sprintf("%s %s", typeName, description))
Expand Down
5 changes: 4 additions & 1 deletion http/jsonapi/generator/generate_security.go
Expand Up @@ -112,7 +112,10 @@ func (g *Generator) buildSecurityConfigs(schema *openapi3.Swagger) error {
if e, ok := value.Value.Extensions["openIdConnectUrl"]; ok {
var url string
if data, ok := e.(json.RawMessage); ok {
json.Unmarshal(data, &url)
err := json.Unmarshal(data, &url)
if err != nil {
return err
}
instanceVal[jen.Id("OpenIdConnectURL")] = jen.Lit(url)
}
}
Expand Down
@@ -1,4 +1,4 @@
package http
package middleware

import (
"errors"
Expand All @@ -9,15 +9,15 @@ import (
"github.com/pace/bricks/maintenance/log"
)

type jsonApiErrorWriter struct {
type errorMiddleware struct {
http.ResponseWriter
req *http.Request
statusCode int
hasErr bool
hasBytes bool
}

func (e *jsonApiErrorWriter) Write(b []byte) (int, error) {
func (e *errorMiddleware) Write(b []byte) (int, error) {
if e.hasErr {
log.Req(e.req).Warn().Msgf("Error already sent, ignoring: %q", string(b))
return 0, nil
Expand All @@ -41,16 +41,16 @@ func (e *jsonApiErrorWriter) Write(b []byte) (int, error) {
return n, err
}

func (e *jsonApiErrorWriter) WriteHeader(code int) {
func (e *errorMiddleware) WriteHeader(code int) {
e.statusCode = code
e.ResponseWriter.WriteHeader(code)
}

// JsonApiErrorWriterMiddleware is a middleware that wraps http.ResponseWriter
// ErrorMiddleware is a middleware that wraps http.ResponseWriter
// such that it forces responses with status codes 4xx/5xx to have
// Content-Type: application/vnd.api+json
func JsonApiErrorWriterMiddleware(next http.Handler) http.Handler {
func Error(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(&jsonApiErrorWriter{ResponseWriter: w, req: r}, r)
next.ServeHTTP(&errorMiddleware{ResponseWriter: w, req: r}, r)
})
}
@@ -1,4 +1,4 @@
package http
package middleware

import (
"encoding/json"
Expand All @@ -14,7 +14,7 @@ import (

const payload = "dummy response data"

func TestJsonApiErrorMiddleware(t *testing.T) {
func TestErrorMiddleware(t *testing.T) {
for _, statusCode := range []int{200, 201, 400, 402, 500, 503} {
for _, responseContentType := range []string{"text/plain", "text/html", runtime.JSONAPIContentType} {
r := mux.NewRouter()
Expand All @@ -23,7 +23,7 @@ func TestJsonApiErrorMiddleware(t *testing.T) {
w.WriteHeader(statusCode)
_, _ = io.WriteString(w, payload)
}).Methods("GET")
r.Use(JsonApiErrorWriterMiddleware)
r.Use(Error)

rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/foo", nil)
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestJsonApiErrorMiddlewareMultipleErrorWrite(t *testing.T) {
if _, err := io.WriteString(w, payload); err != nil {
t.Fatal(err)
}
if jsonWriter, ok := w.(*jsonApiErrorWriter); ok && !jsonWriter.hasErr {
if jsonWriter, ok := w.(*errorMiddleware); ok && !jsonWriter.hasErr {
t.Fatal("expected hasErr flag to be set")
}
if _, err := io.WriteString(w, payload); err != nil {
Expand All @@ -84,7 +84,7 @@ func TestJsonApiErrorMiddlewareMultipleErrorWrite(t *testing.T) {
t.Fatal(err)
}
}).Methods("GET")
r.Use(JsonApiErrorWriterMiddleware)
r.Use(Error)

rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/foo", nil)
Expand Down Expand Up @@ -117,15 +117,15 @@ func TestJsonApiErrorMiddlewareInvalidWriteOrder(t *testing.T) {
if _, err := io.WriteString(w, payload); err != nil {
t.Fatal(err)
}
jsonWriter, ok := w.(*jsonApiErrorWriter)
jsonWriter, ok := w.(*errorMiddleware)
if ok && !jsonWriter.hasBytes {
t.Fatal("expected hasBytes flag to be set")
}
w.WriteHeader(400)
w.Header().Set("Content-Type", "text/plain")
_, _ = io.WriteString(w, payload) // will get discarded
}).Methods("GET")
r.Use(JsonApiErrorWriterMiddleware)
r.Use(Error)

rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/foo", nil)
Expand Down
8 changes: 4 additions & 4 deletions http/jsonapi/request.go
Expand Up @@ -274,8 +274,8 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)

buf := bytes.NewBuffer(nil)

json.NewEncoder(buf).Encode(data.Relationships[args[1]])
json.NewDecoder(buf).Decode(relationship)
json.NewEncoder(buf).Encode(data.Relationships[args[1]]) // nolint: errcheck
json.NewDecoder(buf).Decode(relationship) // nolint: errcheck

data := relationship.Data
models := reflect.New(fieldValue.Type()).Elem()
Expand All @@ -302,10 +302,10 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)

buf := bytes.NewBuffer(nil)

json.NewEncoder(buf).Encode(
json.NewEncoder(buf).Encode( // nolint: errcheck
data.Relationships[args[1]],
)
json.NewDecoder(buf).Decode(relationship)
json.NewDecoder(buf).Decode(relationship) // nolint: errcheck

/*
http://jsonapi.org/format/#document-resource-object-relationships
Expand Down
40 changes: 29 additions & 11 deletions http/jsonapi/request_test.go
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
"log"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -202,7 +203,6 @@ func TestUnmarshalToStructWithPointerAttr_BadType_Struct(t *testing.T) {

func TestUnmarshalToStructWithPointerAttr_BadType_IntSlice(t *testing.T) {
out := new(WithPointer)
type FooStruct struct{ A, B int }
in := map[string]json.RawMessage{
"name": json.RawMessage(`[4, 5]`), // This is the wrong type.
}
Expand Down Expand Up @@ -361,7 +361,10 @@ func TestUnmarshalParsesISO8601(t *testing.T) {
}

in := bytes.NewBuffer(nil)
json.NewEncoder(in).Encode(payload)
err := json.NewEncoder(in).Encode(payload)
if err != nil {
log.Fatal(err)
}

out := new(Timestamp)

Expand All @@ -387,7 +390,10 @@ func TestUnmarshalParsesISO8601TimePointer(t *testing.T) {
}

in := bytes.NewBuffer(nil)
json.NewEncoder(in).Encode(payload)
err := json.NewEncoder(in).Encode(payload)
if err != nil {
t.Fatal(err)
}

out := new(Timestamp)

Expand All @@ -413,7 +419,10 @@ func TestUnmarshalInvalidISO8601(t *testing.T) {
}

in := bytes.NewBuffer(nil)
json.NewEncoder(in).Encode(payload)
err := json.NewEncoder(in).Encode(payload)
if err != nil {
t.Fatal(err)
}

out := new(Timestamp)

Expand Down Expand Up @@ -969,7 +978,7 @@ func samplePayload() io.Reader {
}

out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
json.NewEncoder(out).Encode(payload) // nolint: errcheck

return out
}
Expand All @@ -987,7 +996,7 @@ func samplePayloadWithID() io.Reader {
}

out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
json.NewEncoder(out).Encode(payload) // nolint: errcheck

return out
}
Expand All @@ -1002,7 +1011,7 @@ func samplePayloadWithBadTypes(m map[string]json.RawMessage) io.Reader {
}

out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
json.NewEncoder(out).Encode(payload) // nolint: errcheck

return out
}
Expand All @@ -1017,7 +1026,7 @@ func sampleWithPointerPayload(m map[string]json.RawMessage) io.Reader {
}

out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
json.NewEncoder(out).Encode(payload) // nolint: errcheck

return out
}
Expand Down Expand Up @@ -1094,17 +1103,26 @@ func samplePayloadWithSideloaded() io.Reader {
testModel := testModel()

out := bytes.NewBuffer(nil)
MarshalPayload(out, testModel)
err := MarshalPayload(out, testModel)
if err != nil {
panic(err)
}

return out
}

func sampleSerializedEmbeddedTestModel() *Blog {
out := bytes.NewBuffer(nil)
MarshalOnePayloadEmbedded(out, testModel())
err := MarshalOnePayloadEmbedded(out, testModel())
if err != nil {
panic(err)
}

blog := new(Blog)
UnmarshalPayload(out, blog)
err = UnmarshalPayload(out, blog)
if err != nil {
panic(err)
}

return blog
}
Expand Down
12 changes: 9 additions & 3 deletions http/jsonapi/response_test.go
Expand Up @@ -27,7 +27,10 @@ func TestMarshalPayload(t *testing.T) {

// One
out1 := bytes.NewBuffer(nil)
MarshalPayload(out1, book)
err := MarshalPayload(out1, book)
if err != nil {
t.Fatal(err)
}

if strings.Contains(out1.String(), `"9.9999999999999999999"`) {
t.Fatalf("decimals should be encoded as number, got: %q", out1.String())
Expand All @@ -39,11 +42,14 @@ func TestMarshalPayload(t *testing.T) {
if _, ok := jsonData["data"].(map[string]interface{}); !ok {
t.Fatalf("data key did not contain an Hash/Dict/Map")
}
fmt.Println(string(out1.Bytes()))
fmt.Println(out1.String())

// Many
out2 := bytes.NewBuffer(nil)
MarshalPayload(out2, books)
err = MarshalPayload(out2, books)
if err != nil {
t.Fatal(err)
}

if err := json.Unmarshal(out2.Bytes(), &jsonData); err != nil {
t.Fatal(err)
Expand Down
8 changes: 4 additions & 4 deletions http/jsonapi/runtime.go
Expand Up @@ -79,13 +79,13 @@ func (r *Runtime) UnmarshalPayload(reader io.Reader, model interface{}) error {
}

// UnmarshalManyPayload has docs in request.go for UnmarshalManyPayload.
func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (elems []interface{}, err error) {
r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
elems, err = UnmarshalManyPayload(reader, kind)
func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (elements []interface{}, err error) {
err2 := r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
elements, err = UnmarshalManyPayload(reader, kind)
return err
})

return
return elements, err2
}

// MarshalPayload has docs in response.go for MarshalPayload.
Expand Down
8 changes: 0 additions & 8 deletions http/jsonapi/runtime/error.go
Expand Up @@ -111,14 +111,6 @@ func WriteError(w http.ResponseWriter, code int, err error) {
log.Logger().Info().Str("req_id", reqID).
Err(err).Msg("Unable to send error response to the client")
}

// log all errors send to the client
for _, ei := range errList.List {
ev := log.Logger().Info().Str("req_id", reqID)
if source := ei.Source; source != nil {
ev = ev.Fields(*source)
}
}
}

// Error objects MUST be returned as an array keyed by errors in the top level of a JSON API document.
Expand Down

0 comments on commit 0190d91

Please sign in to comment.