]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: refactor body logic in test
authorRuss Cox <rsc@golang.org>
Tue, 22 May 2012 17:46:53 +0000 (13:46 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 22 May 2012 17:46:53 +0000 (13:46 -0400)
This just eliminates some duplication.
Also add a pointer to RFC 1122, in case
this comes up again.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6229044

src/pkg/net/http/serve_test.go

index db1cbbbf1257cf2458a1e864f66b51c928fc52bf..196b1ac361ceeaf163119602a085b9cc6cd816aa 100644 (file)
@@ -618,13 +618,6 @@ type serverExpectTest struct {
        expectedResponse string // expected substring in first line of http response
 }
 
-// forcedBadBody returns whether this test sends an unsolicited body
-// without asking the server's permission and which we know the server
-// will deny (possibly before we finish writing the body).
-func (t serverExpectTest) forcedBadBody() bool {
-       return t.contentLength > 0 && !t.readBody && strings.ToLower(t.expectation) != "100-continue"
-}
-
 var serverExpectTests = []serverExpectTest{
        // Normal 100-continues, case-insensitive.
        {100, "100-continue", true, "100 Continue"},
@@ -668,6 +661,11 @@ func TestServerExpect(t *testing.T) {
                        t.Fatalf("Dial: %v", err)
                }
                defer conn.Close()
+
+               // Only send the body immediately if we're acting like an HTTP client
+               // that doesn't send 100-continue expectations.
+               writeBody := test.contentLength > 0 && strings.ToLower(test.expectation) != "100-continue"
+
                go func() {
                        _, err := fmt.Fprintf(conn, "POST /?readbody=%v HTTP/1.1\r\n"+
                                "Connection: close\r\n"+
@@ -678,13 +676,11 @@ func TestServerExpect(t *testing.T) {
                                t.Errorf("On test %#v, error writing request headers: %v", test, err)
                                return
                        }
-                       // Only send the body immediately if we're acting like an HTTP client
-                       // that doesn't send 100-continue expectations.
-                       if test.contentLength > 0 && strings.ToLower(test.expectation) != "100-continue" {
+                       if writeBody {
                                body := strings.Repeat("A", test.contentLength)
                                _, err = fmt.Fprint(conn, body)
                                if err != nil {
-                                       if test.forcedBadBody() {
+                                       if !test.readBody {
                                                // Server likely already hung up on us.
                                                // See larger comment below.
                                                t.Logf("On test %#v, acceptable error writing request body: %v", test, err)
@@ -697,11 +693,12 @@ func TestServerExpect(t *testing.T) {
                bufr := bufio.NewReader(conn)
                line, err := bufr.ReadString('\n')
                if err != nil {
-                       if test.forcedBadBody() {
+                       if writeBody && !test.readBody {
                                // This is an acceptable failure due to a possible TCP race:
                                // We were still writing data and the server hung up on us. A TCP
                                // implementation may send a RST if our request body data was known
                                // to be lost, which may trigger our reads to fail.
+                               // See RFC 1122 page 88.
                                t.Logf("On test %#v, acceptable error from ReadString: %v", test, err)
                                return
                        }