net/http/httputil: fix unannounced trailers when body is empty

Fix unannounced trailers when body is empty and without announced trailers.

Fixes #29031

Change-Id: If49951a42fe56d4be4436a999627db4c2678659d
GitHub-Last-Rev: 3469adc8f5
GitHub-Pull-Request: golang/go#29032
Reviewed-on: https://go-review.googlesource.com/c/151898
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
SALLEYRON Julien 2018-12-03 20:46:23 +00:00 committed by Brad Fitzpatrick
parent a48a15cd50
commit 48399cae9f
2 changed files with 50 additions and 18 deletions

View file

@ -7,23 +7,23 @@
package httputil
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"reflect"
"strconv"
"strings"
"sync"
"testing"
"time"
"reflect"
"io"
"strings"
"bufio"
"sync"
"strconv"
"bytes"
"errors"
"fmt"
"os"
)
const fakeHopHeader = "X-Fake-Hop-Header-For-Test"
@ -1048,3 +1048,33 @@ func TestReverseProxyWebSocket(t *testing.T) {
t.Errorf("got %#q, want %#q", got, want)
}
}
func TestUnannouncedTrailer(t *testing.T) {
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.(http.Flusher).Flush()
w.Header().Set(http.TrailerPrefix+"X-Unannounced-Trailer", "unannounced_trailer_value")
}))
defer backend.Close()
backendURL, err := url.Parse(backend.URL)
if err != nil {
t.Fatal(err)
}
proxyHandler := NewSingleHostReverseProxy(backendURL)
proxyHandler.ErrorLog = log.New(ioutil.Discard, "", 0) // quiet for tests
frontend := httptest.NewServer(proxyHandler)
defer frontend.Close()
frontendClient := frontend.Client()
res, err := frontendClient.Get(frontend.URL)
if err != nil {
t.Fatalf("Get: %v", err)
}
ioutil.ReadAll(res.Body)
if g, w := res.Trailer.Get("X-Unannounced-Trailer"), "unannounced_trailer_value"; g != w {
t.Errorf("Trailer(X-Unannounced-Trailer) = %q; want %q", g, w)
}
}