Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http/httptest: ResponseRecorder does not implement http.ResponseWriter spec correctly #12986

Closed
srinathh opened this issue Oct 19, 2015 · 4 comments

Comments

@srinathh
Copy link
Contributor

The documentation for http.ResponseWriter specifies certain behaviors for the Header() Header and the Write([]byte) (int, error) functions that are not being implemented by httptest.ResponseRecorder currently. Specifically -

  • in Write() If the Header does not contain a Content-Type line, Write adds a Content-Type set to the result of passing the initial 512 bytes of written data to DetectContentType.
  • Changing the header after a call to WriteHeader (or Write) has no effect unless the modified headers were declared as trailers by setting the "Trailer" header before the call to WriteHeader

I ran into this issue when looking for Content-Type header from ResponseRecorder and looking at the code for ResponseRecorder, did not see an implementation for the spec.

@bradfitz bradfitz added this to the Unplanned milestone Oct 19, 2015
@nodirt
Copy link
Contributor

nodirt commented Oct 19, 2015

I will take this one

@gopherbot
Copy link

CL https://golang.org/cl/16096 mentions this issue.

@nodirt
Copy link
Contributor

nodirt commented Oct 20, 2015

I think ResponseRecorder already has enough to check trailers.

// trailers returns only trailer headers.
func trailers(h http.Header) http.Header {
    res := http.Header{}
    for _, keyString := range h["Trailer"] {
        for _, key := range strings.Split(keyString, ",") {
            key = http.CanonicalHeaderKey(strings.TrimSpace(key))
            res[key] = h[key]
        }
    }
    return res
}

This will return only trailers. https://play.golang.org/p/348R0WDFOM

bradfitz pushed a commit that referenced this issue Oct 20, 2015
* detect Content-Type on ReponseRecorder.Write[String] call
  if header wasn't written yet, Content-Type header is not set and
  Transfer-Encoding is not set.
* fix typos in serve_test.go

Updates #12986

Change-Id: Id2ed8b1994e64657370fed71eb3882d611f76b31
Reviewed-on: https://go-review.googlesource.com/16096
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@nodirt
Copy link
Contributor

nodirt commented Oct 20, 2015

I think this can be closed.

@rakyll rakyll closed this as completed Oct 20, 2015
bradfitz added a commit that referenced this issue Dec 10, 2015
Fixes some failing Google tests when run under Go tip (1.6).

Updates #12986

Change-Id: I0ca4d20f6103d10ea9464e45730085401336dada
Reviewed-on: https://go-review.googlesource.com/17698
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Nodir Turakulov <nodir@google.com>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
@golang golang locked and limited conversation to collaborators Oct 24, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants