From 617e2d1fd1e95d9c17ab7978b2e28e4553787751 Mon Sep 17 00:00:00 2001 From: Frank Mueller Date: Thu, 16 Dec 2021 22:05:06 +0100 Subject: [PATCH] Extend web package (#17) * Change and extend web body helpers * Fix linter errors * Fix linter errors * Fix web response status * Extend documentation --- CHANGELOG.md | 10 ++++++++++ web/doc.go | 22 ++++++++++++++++++---- web/web.go | 3 ++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44abdcd..6891150 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## v0.6.2 + +* (C) Migrate web response to http.Response +* (A) Add helper for request body + +## v0.6.1 + +* (C) Change web response to use the standard http.Response +* (A) Add helper for response body + ## v0.6.0 * (A) New web package for handler tests diff --git a/web/doc.go b/web/doc.go index e161bfb..dde6946 100644 --- a/web/doc.go +++ b/web/doc.go @@ -5,10 +5,24 @@ // All rights reserved. Use of this source code is governed // by the new BSD license. -// Package web helps testing web handlers. Those can be registered, -// standard web requests can be sent for execution and a response collects -// the response for analysis. Automation helps to execute steps upfront -// passing the request to the handler. +// Package web helps testing web handlers. A simulator can be started +// with the handler to test. Then standard http requests can be sent +// and the returned http responses can be analyzed. +// +// h := NewMyHandler() +// s := web.NewSimulator(h) +// +// req, err := http.NewRequest(http.MethodGet, "http://localhost:8080/", nil) +// assert.NoError(err) +// +// resp, err := s.Do(req) +// assert.NoError(err) +// assert.Equal(resp.StatusCode, http.StatusOK) +// body, err := web.BodyToString(resp) +// assert.NoError(err) +// assert.Equal(body, test.expected) +// +// Some smaller functions help working with the requests and responses. package web // import "tideland.dev/go/audit/web" // EOF diff --git a/web/web.go b/web/web.go index 503e62f..5703d60 100644 --- a/web/web.go +++ b/web/web.go @@ -14,6 +14,7 @@ package web // import "tideland.dev/go/audit/web" import ( "bytes" "encoding/json" + "fmt" "io/ioutil" "net/http" ) @@ -33,7 +34,6 @@ func newResponseWriter() *ResponseWriter { w := &ResponseWriter{ buffer: bytes.NewBuffer(nil), resp: &http.Response{ - Status: "200 OK", StatusCode: http.StatusOK, Proto: "HTTP/1.0", ProtoMajor: 1, @@ -65,6 +65,7 @@ func (w *ResponseWriter) Write(bs []byte) (int, error) { // finalize finalizes the usage of the response writer. func (w *ResponseWriter) finalize(r *http.Request) { + w.resp.Status = fmt.Sprintf("%d %s", w.resp.StatusCode, http.StatusText(w.resp.StatusCode)) w.resp.ContentLength = int64(w.buffer.Len()) w.resp.Request = r }