Selaa lähdekoodia

Fix bug with cut-off responses

Lu Stadler 7 vuotta sitten
vanhempi
commit
4372a53fd2
1 muutettua tiedostoa jossa 10 lisäystä ja 10 poistoa
  1. 10 10
      go/fake-http/fake-http.go

+ 10 - 10
go/fake-http/fake-http.go

@ -72,16 +72,6 @@ func main() {
72 72
		userAgent := req.Header.Get("User-Agent")
73 73
		log.Printf("%s %s - %d (%s, %q)", req.Method, req.URL, resp.StatusCode, req.RemoteAddr, userAgent)
74 74
75
		if resp.Header.Get("Content-Type") == "application/json" {
76
			pretty, err := prettyfyJSON(resp.Body)
77
			if err != nil {
78
				log.Printf("Error: Prettyfying JSON: %s", err)
79
			} else {
80
				resp.Header.Set("Content-Length", fmt.Sprintf("%d", len(pretty)))
81
				resp.Body = ioutil.NopCloser(bytes.NewReader(pretty))
82
			}
83
		}
84
85 75
		requestLog.Log(req, resp)
86 76
	})
87 77
@ -256,6 +246,15 @@ func (l *Log) Log(req *http.Request, resp *http.Response) {
256 246
		responseBody: new(bytes.Buffer),
257 247
	}
258 248
	io.Copy(e.requestBody, req.Body)
249
	if resp.Header.Get("Content-Type") == "application/json" {
250
		pretty, err := prettyfyJSON(resp.Body)
251
		if err != nil {
252
			log.Printf("Error: Prettyfying JSON: %s", err)
253
		} else {
254
			resp.ContentLength = int64(len(pretty))
255
			resp.Body = ioutil.NopCloser(bytes.NewReader(pretty))
256
		}
257
	}
259 258
	io.Copy(e.responseBody, resp.Body)
260 259
	*l = append(*l, e)
261 260
}
@ -287,6 +286,7 @@ func (e LogEntry) Request() *http.Request {
287 286
// Response returns the stored http.Response.
288 287
func (e LogEntry) Response() *http.Response {
289 288
	e.response.Body = ioutil.NopCloser(bytes.NewReader(e.responseBody.Bytes()))
289
	e.response.ContentLength = int64(e.responseBody.Len())
290 290
	return e.response
291 291
}
292 292