Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 10, 2019
2 parents f33a304 + 88c6b86 commit d96608f
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 217 deletions.
14 changes: 12 additions & 2 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ func (a *api) get(resource string, query url.Values, result interface{}) error {
}

func (a *api) post(resource string, body interface{}, result interface{}) error {
b, err := json.Marshal(body)
v := body
if v == nil {
v = struct{}{}
}

b, err := json.Marshal(v)
if err != nil {
return err
}
Expand Down Expand Up @@ -100,7 +105,12 @@ func (a *api) post(resource string, body interface{}, result interface{}) error
}

func (a *api) patch(resource string, body interface{}, result interface{}) error {
b, err := json.Marshal(body)
v := body
if v == nil {
v = struct{}{}
}

b, err := json.Marshal(v)
if err != nil {
return err
}
Expand Down
112 changes: 103 additions & 9 deletions mite/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package mite_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)

const (
Expand All @@ -11,18 +13,110 @@ const (
testUserAgent = "mite-go/" + testClientVersion + " (+github.com/leanovate/mite-go)"
)

type recorder struct {
method string
url string
body []byte
contentType string
userAgent string
miteKey string
type Recorder struct {
reqMethod string
reqUri string
reqBody []byte
reqHeader http.Header
resHeader http.Header
resBody string
resStatus int
}

func prettifyJson(b []byte, indent string) []byte {
func NewRecorder() *Recorder {
return &Recorder{reqHeader: make(http.Header), resHeader: make(http.Header), resStatus: 200}
}

func (r *Recorder) RequestMethod() string {
return r.reqMethod
}

func (r *Recorder) RequestURI() string {
return r.reqUri
}

func (r *Recorder) RequestContentType() string {
return r.RequestHeader("Content-Type")
}

func (r *Recorder) RequestUserAgent() string {
return r.RequestHeader("User-Agent")
}

func (r *Recorder) RequestMiteKey() string {
return r.RequestHeader("X-MiteApiKey")
}

func (r *Recorder) RequestHeader(header string) string {
return r.reqHeader.Get(header)
}

func (r *Recorder) RequestBody() []byte {
return r.reqBody
}

func (r *Recorder) RequestBodyCanonical() string {
return string(canonicalJSON(r.reqBody))
}

func (r *Recorder) ResponseContentType(contentType string) *Recorder {
r.resHeader.Add("Content-Type", contentType)
return r
}

func (r *Recorder) ResponseLocation(location string) *Recorder {
r.resHeader.Add("Location", location)
return r
}

func (r *Recorder) ResponseBody(body string) *Recorder {
r.resBody = body
return r
}

func (r *Recorder) ResponseStatus(status int) *Recorder {
r.resStatus = status
return r
}

func (r *Recorder) Handler() http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
b, err := ioutil.ReadAll(request.Body)
if err != nil {
panic(err)
}

r.reqMethod = request.Method
r.reqUri = request.RequestURI
r.reqHeader = request.Header
r.reqBody = b

for k, vs := range r.resHeader {
for _, v := range vs {
writer.Header().Add(k, v)
}
}

writer.WriteHeader(r.resStatus)

if r.resBody == "" {
return
}

_, err = writer.Write([]byte(r.resBody))
if err != nil {
panic(err)
}
})
}

func CanonicalString(s string) string {
return string(canonicalJSON([]byte(s)))
}

func canonicalJSON(b []byte) []byte {
buf := &bytes.Buffer{}
err := json.Indent(buf, b, "", indent)
err := json.Indent(buf, b, "", " ")
if err != nil {
panic(err)
}
Expand Down
26 changes: 11 additions & 15 deletions mite/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,11 @@ var projectObject = domain.Project{

func TestApi_Projects(t *testing.T) {
// given
rec := recorder{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec.method = r.Method
rec.url = r.RequestURI
rec.miteKey = r.Header.Get("X-MiteApiKey")
rec.userAgent = r.Header.Get("User-Agent")

w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
_, _ = w.Write([]byte(fmt.Sprintf("[%s]", projectResponse)))
}))
rec := NewRecorder().
ResponseContentType("application/json; charset=utf-8").
ResponseBody(fmt.Sprintf("[%s]", projectResponse)).
ResponseStatus(200)
srv := httptest.NewServer(rec.Handler())

defer srv.Close()

Expand All @@ -69,8 +63,10 @@ func TestApi_Projects(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, []*domain.Project{&projectObject}, projects)

assert.Equal(t, http.MethodGet, rec.method)
assert.Equal(t, "/projects.json", rec.url)
assert.Equal(t, testApiKey, rec.miteKey)
assert.Equal(t, testUserAgent, rec.userAgent)
assert.Equal(t, http.MethodGet, rec.RequestMethod())
assert.Equal(t, "/projects.json", rec.RequestURI())
assert.Empty(t, rec.RequestContentType())
assert.Equal(t, testUserAgent, rec.RequestUserAgent())
assert.Equal(t, testApiKey, rec.RequestMiteKey())
assert.Empty(t, rec.RequestBody())
}
26 changes: 11 additions & 15 deletions mite/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,11 @@ var serviceObject = domain.Service{

func TestApi_Services(t *testing.T) {
// given
rec := recorder{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec.method = r.Method
rec.url = r.RequestURI
rec.miteKey = r.Header.Get("X-MiteApiKey")
rec.userAgent = r.Header.Get("User-Agent")

w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
_, _ = w.Write([]byte(fmt.Sprintf("[%s]", serviceResponse)))
}))
rec := NewRecorder().
ResponseContentType("application/json; charset=utf-8").
ResponseBody(fmt.Sprintf("[%s]", serviceResponse)).
ResponseStatus(200)
srv := httptest.NewServer(rec.Handler())

defer srv.Close()

Expand All @@ -55,8 +49,10 @@ func TestApi_Services(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, []*domain.Service{&serviceObject}, services)

assert.Equal(t, http.MethodGet, rec.method)
assert.Equal(t, "/services.json", rec.url)
assert.Equal(t, testApiKey, rec.miteKey)
assert.Equal(t, testUserAgent, rec.userAgent)
assert.Equal(t, http.MethodGet, rec.RequestMethod())
assert.Equal(t, "/services.json", rec.RequestURI())
assert.Empty(t, rec.RequestContentType())
assert.Equal(t, testUserAgent, rec.RequestUserAgent())
assert.Equal(t, testApiKey, rec.RequestMiteKey())
assert.Empty(t, rec.RequestBody())
}
Loading

0 comments on commit d96608f

Please sign in to comment.