OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [http/] [httputil/] [dump_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package httputil
6
 
7
import (
8
        "bytes"
9
        "fmt"
10
        "io"
11
        "io/ioutil"
12
        "net/http"
13
        "net/url"
14
        "testing"
15
)
16
 
17
type dumpTest struct {
18
        Req  http.Request
19
        Body interface{} // optional []byte or func() io.ReadCloser to populate Req.Body
20
 
21
        WantDump    string
22
        WantDumpOut string
23
}
24
 
25
var dumpTests = []dumpTest{
26
 
27
        // HTTP/1.1 => chunked coding; body; empty trailer
28
        {
29
                Req: http.Request{
30
                        Method: "GET",
31
                        URL: &url.URL{
32
                                Scheme: "http",
33
                                Host:   "www.google.com",
34
                                Path:   "/search",
35
                        },
36
                        ProtoMajor:       1,
37
                        ProtoMinor:       1,
38
                        TransferEncoding: []string{"chunked"},
39
                },
40
 
41
                Body: []byte("abcdef"),
42
 
43
                WantDump: "GET /search HTTP/1.1\r\n" +
44
                        "Host: www.google.com\r\n" +
45
                        "Transfer-Encoding: chunked\r\n\r\n" +
46
                        chunk("abcdef") + chunk(""),
47
        },
48
 
49
        // Verify that DumpRequest preserves the HTTP version number, doesn't add a Host,
50
        // and doesn't add a User-Agent.
51
        {
52
                Req: http.Request{
53
                        Method:     "GET",
54
                        URL:        mustParseURL("/foo"),
55
                        ProtoMajor: 1,
56
                        ProtoMinor: 0,
57
                        Header: http.Header{
58
                                "X-Foo": []string{"X-Bar"},
59
                        },
60
                },
61
 
62
                WantDump: "GET /foo HTTP/1.0\r\n" +
63
                        "X-Foo: X-Bar\r\n\r\n",
64
        },
65
 
66
        {
67
                Req: *mustNewRequest("GET", "http://example.com/foo", nil),
68
 
69
                WantDumpOut: "GET /foo HTTP/1.1\r\n" +
70
                        "Host: example.com\r\n" +
71
                        "User-Agent: Go http package\r\n" +
72
                        "Accept-Encoding: gzip\r\n\r\n",
73
        },
74
}
75
 
76
func TestDumpRequest(t *testing.T) {
77
        for i, tt := range dumpTests {
78
                setBody := func() {
79
                        if tt.Body == nil {
80
                                return
81
                        }
82
                        switch b := tt.Body.(type) {
83
                        case []byte:
84
                                tt.Req.Body = ioutil.NopCloser(bytes.NewBuffer(b))
85
                        case func() io.ReadCloser:
86
                                tt.Req.Body = b()
87
                        }
88
                }
89
                setBody()
90
                if tt.Req.Header == nil {
91
                        tt.Req.Header = make(http.Header)
92
                }
93
 
94
                if tt.WantDump != "" {
95
                        setBody()
96
                        dump, err := DumpRequest(&tt.Req, true)
97
                        if err != nil {
98
                                t.Errorf("DumpRequest #%d: %s", i, err)
99
                                continue
100
                        }
101
                        if string(dump) != tt.WantDump {
102
                                t.Errorf("DumpRequest %d, expecting:\n%s\nGot:\n%s\n", i, tt.WantDump, string(dump))
103
                                continue
104
                        }
105
                }
106
 
107
                if tt.WantDumpOut != "" {
108
                        setBody()
109
                        dump, err := DumpRequestOut(&tt.Req, true)
110
                        if err != nil {
111
                                t.Errorf("DumpRequestOut #%d: %s", i, err)
112
                                continue
113
                        }
114
                        if string(dump) != tt.WantDumpOut {
115
                                t.Errorf("DumpRequestOut %d, expecting:\n%s\nGot:\n%s\n", i, tt.WantDumpOut, string(dump))
116
                                continue
117
                        }
118
                }
119
        }
120
}
121
 
122
func chunk(s string) string {
123
        return fmt.Sprintf("%x\r\n%s\r\n", len(s), s)
124
}
125
 
126
func mustParseURL(s string) *url.URL {
127
        u, err := url.Parse(s)
128
        if err != nil {
129
                panic(fmt.Sprintf("Error parsing URL %q: %v", s, err))
130
        }
131
        return u
132
}
133
 
134
func mustNewRequest(method, url string, body io.Reader) *http.Request {
135
        req, err := http.NewRequest(method, url, body)
136
        if err != nil {
137
                panic(fmt.Sprintf("NewRequest(%q, %q, %p) err = %v", method, url, body, err))
138
        }
139
        return req
140
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.