Skip to content

Commit

Permalink
Extend web package (#17)
Browse files Browse the repository at this point in the history
* Change and extend web body helpers

* Fix linter errors

* Fix linter errors

* Fix web response status

* Extend documentation
  • Loading branch information
themue authored Dec 16, 2021
1 parent 0bbd5bd commit 617e2d1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 18 additions & 4 deletions web/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package web // import "tideland.dev/go/audit/web"
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 617e2d1

Please sign in to comment.