You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

560 lines
17 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>httptest - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package httptest</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "net/http/httptest"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package httptest provides utilities for HTTP testing.
  69. </p>
  70. </div>
  71. </div>
  72. <div id="pkg-index" class="toggleVisible">
  73. <div class="collapsed">
  74. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  75. </div>
  76. <div class="expanded">
  77. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  78. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  79. <div id="manual-nav">
  80. <dl>
  81. <dd><a href="index.html#pkg-constants">Constants</a></dd>
  82. <dd><a href="index.html#ResponseRecorder">type ResponseRecorder</a></dd>
  83. <dd>&nbsp; &nbsp; <a href="index.html#NewRecorder">func NewRecorder() *ResponseRecorder</a></dd>
  84. <dd>&nbsp; &nbsp; <a href="index.html#ResponseRecorder.Flush">func (rw *ResponseRecorder) Flush()</a></dd>
  85. <dd>&nbsp; &nbsp; <a href="index.html#ResponseRecorder.Header">func (rw *ResponseRecorder) Header() http.Header</a></dd>
  86. <dd>&nbsp; &nbsp; <a href="index.html#ResponseRecorder.Write">func (rw *ResponseRecorder) Write(buf []byte) (int, error)</a></dd>
  87. <dd>&nbsp; &nbsp; <a href="index.html#ResponseRecorder.WriteHeader">func (rw *ResponseRecorder) WriteHeader(code int)</a></dd>
  88. <dd>&nbsp; &nbsp; <a href="index.html#ResponseRecorder.WriteString">func (rw *ResponseRecorder) WriteString(str string) (int, error)</a></dd>
  89. <dd><a href="index.html#Server">type Server</a></dd>
  90. <dd>&nbsp; &nbsp; <a href="index.html#NewServer">func NewServer(handler http.Handler) *Server</a></dd>
  91. <dd>&nbsp; &nbsp; <a href="index.html#NewTLSServer">func NewTLSServer(handler http.Handler) *Server</a></dd>
  92. <dd>&nbsp; &nbsp; <a href="index.html#NewUnstartedServer">func NewUnstartedServer(handler http.Handler) *Server</a></dd>
  93. <dd>&nbsp; &nbsp; <a href="index.html#Server.Close">func (s *Server) Close()</a></dd>
  94. <dd>&nbsp; &nbsp; <a href="index.html#Server.CloseClientConnections">func (s *Server) CloseClientConnections()</a></dd>
  95. <dd>&nbsp; &nbsp; <a href="index.html#Server.Start">func (s *Server) Start()</a></dd>
  96. <dd>&nbsp; &nbsp; <a href="index.html#Server.StartTLS">func (s *Server) StartTLS()</a></dd>
  97. </dl>
  98. </div><!-- #manual-nav -->
  99. <div id="pkg-examples">
  100. <h4>Examples</h4>
  101. <dl>
  102. <dd><a class="exampleLink" href="index.html#example_ResponseRecorder">ResponseRecorder</a></dd>
  103. <dd><a class="exampleLink" href="index.html#example_Server">Server</a></dd>
  104. </dl>
  105. </div>
  106. <h4>Package files</h4>
  107. <p>
  108. <span style="font-size:90%">
  109. <a href="http://localhost:6060/src/net/http/httptest/recorder.go">recorder.go</a>
  110. <a href="http://localhost:6060/src/net/http/httptest/server.go">server.go</a>
  111. </span>
  112. </p>
  113. </div><!-- .expanded -->
  114. </div><!-- #pkg-index -->
  115. <div id="pkg-callgraph" class="toggle" style="display: none">
  116. <div class="collapsed">
  117. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  118. </div> <!-- .expanded -->
  119. <div class="expanded">
  120. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  121. <p>
  122. In the call graph viewer below, each node
  123. is a function belonging to this package
  124. and its children are the functions it
  125. calls&mdash;perhaps dynamically.
  126. </p>
  127. <p>
  128. The root nodes are the entry points of the
  129. package: functions that may be called from
  130. outside the package.
  131. There may be non-exported or anonymous
  132. functions among them if they are called
  133. dynamically from another package.
  134. </p>
  135. <p>
  136. Click a node to visit that function's source code.
  137. From there you can visit its callers by
  138. clicking its declaring <code>func</code>
  139. token.
  140. </p>
  141. <p>
  142. Functions may be omitted if they were
  143. determined to be unreachable in the
  144. particular programs or tests that were
  145. analyzed.
  146. </p>
  147. <!-- Zero means show all package entry points. -->
  148. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  149. </div>
  150. </div> <!-- #pkg-callgraph -->
  151. <h2 id="pkg-constants">Constants</h2>
  152. <pre>const <span id="DefaultRemoteAddr">DefaultRemoteAddr</span> = &#34;1.2.3.4&#34;</pre>
  153. <p>
  154. DefaultRemoteAddr is the default remote address to return in RemoteAddr if
  155. an explicit DefaultRemoteAddr isn&#39;t set on ResponseRecorder.
  156. </p>
  157. <h2 id="ResponseRecorder">type <a href="http://localhost:6060/src/net/http/httptest/recorder.go?s=393:665#L5">ResponseRecorder</a></h2>
  158. <pre>type ResponseRecorder struct {
  159. Code <a href="../../../builtin/index.html#int">int</a> <span class="comment">// the HTTP response code from WriteHeader</span>
  160. HeaderMap <a href="../index.html">http</a>.<a href="../index.html#Header">Header</a> <span class="comment">// the HTTP response headers</span>
  161. Body *<a href="../../../bytes/index.html">bytes</a>.<a href="../../../bytes/index.html#Buffer">Buffer</a> <span class="comment">// if non-nil, the bytes.Buffer to append written data to</span>
  162. Flushed <a href="../../../builtin/index.html#bool">bool</a>
  163. <span class="comment">// contains filtered or unexported fields</span>
  164. }</pre>
  165. <p>
  166. ResponseRecorder is an implementation of http.ResponseWriter that
  167. records its mutations for later inspection in tests.
  168. </p>
  169. <div id="example_ResponseRecorder" class="toggle">
  170. <div class="collapsed">
  171. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  172. </div>
  173. <div class="expanded">
  174. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  175. <p>Code:</p>
  176. <pre class="code">handler := func(w http.ResponseWriter, r *http.Request) {
  177. http.Error(w, &#34;something failed&#34;, http.StatusInternalServerError)
  178. }
  179. req, err := http.NewRequest(&#34;GET&#34;, &#34;http://example.com/foo&#34;, nil)
  180. if err != nil {
  181. log.Fatal(err)
  182. }
  183. w := httptest.NewRecorder()
  184. handler(w, req)
  185. fmt.Printf(&#34;%d - %s&#34;, w.Code, w.Body.String())
  186. <span class="comment"></pre>
  187. <p>Output:</p>
  188. <pre class="output">500 - something failed
  189. </pre>
  190. </div>
  191. </div>
  192. <h3 id="NewRecorder">func <a href="http://localhost:6060/src/net/http/httptest/recorder.go?s=723:759#L15">NewRecorder</a></h3>
  193. <pre>func NewRecorder() *<a href="index.html#ResponseRecorder">ResponseRecorder</a></pre>
  194. <p>
  195. NewRecorder returns an initialized ResponseRecorder.
  196. </p>
  197. <h3 id="ResponseRecorder.Flush">func (*ResponseRecorder) <a href="http://localhost:6060/src/net/http/httptest/recorder.go?s=2631:2666#L94">Flush</a></h3>
  198. <pre>func (rw *<a href="index.html#ResponseRecorder">ResponseRecorder</a>) Flush()</pre>
  199. <p>
  200. Flush sets rw.Flushed to true.
  201. </p>
  202. <h3 id="ResponseRecorder.Header">func (*ResponseRecorder) <a href="http://localhost:6060/src/net/http/httptest/recorder.go?s=1096:1144#L28">Header</a></h3>
  203. <pre>func (rw *<a href="index.html#ResponseRecorder">ResponseRecorder</a>) Header() <a href="../index.html">http</a>.<a href="../index.html#Header">Header</a></pre>
  204. <p>
  205. Header returns the response headers.
  206. </p>
  207. <h3 id="ResponseRecorder.Write">func (*ResponseRecorder) <a href="http://localhost:6060/src/net/http/httptest/recorder.go?s=2056:2114#L68">Write</a></h3>
  208. <pre>func (rw *<a href="index.html#ResponseRecorder">ResponseRecorder</a>) Write(buf []<a href="../../../builtin/index.html#byte">byte</a>) (<a href="../../../builtin/index.html#int">int</a>, <a href="../../../builtin/index.html#error">error</a>)</pre>
  209. <p>
  210. Write always succeeds and writes to rw.Body, if not nil.
  211. </p>
  212. <h3 id="ResponseRecorder.WriteHeader">func (*ResponseRecorder) <a href="http://localhost:6060/src/net/http/httptest/recorder.go?s=2476:2525#L86">WriteHeader</a></h3>
  213. <pre>func (rw *<a href="index.html#ResponseRecorder">ResponseRecorder</a>) WriteHeader(code <a href="../../../builtin/index.html#int">int</a>)</pre>
  214. <p>
  215. WriteHeader sets rw.Code.
  216. </p>
  217. <h3 id="ResponseRecorder.WriteString">func (*ResponseRecorder) <a href="http://localhost:6060/src/net/http/httptest/recorder.go?s=2278:2342#L77">WriteString</a></h3>
  218. <pre>func (rw *<a href="index.html#ResponseRecorder">ResponseRecorder</a>) WriteString(str <a href="../../../builtin/index.html#string">string</a>) (<a href="../../../builtin/index.html#int">int</a>, <a href="../../../builtin/index.html#error">error</a>)</pre>
  219. <p>
  220. WriteString always succeeds and writes to rw.Body, if not nil.
  221. </p>
  222. <h2 id="Server">type <a href="http://localhost:6060/src/net/http/httptest/server.go?s=471:1196#L16">Server</a></h2>
  223. <pre>type Server struct {
  224. URL <a href="../../../builtin/index.html#string">string</a> <span class="comment">// base URL of form http://ipaddr:port with no trailing slash</span>
  225. Listener <a href="../../index.html">net</a>.<a href="../../index.html#Listener">Listener</a>
  226. <span class="comment">// TLS is the optional TLS configuration, populated with a new config</span>
  227. <span class="comment">// after TLS is started. If set on an unstarted server before StartTLS</span>
  228. <span class="comment">// is called, existing fields are copied into the new config.</span>
  229. TLS *<a href="../../../crypto/tls/index.html">tls</a>.<a href="../../../crypto/tls/index.html#Config">Config</a>
  230. <span class="comment">// Config may be changed after calling NewUnstartedServer and</span>
  231. <span class="comment">// before Start or StartTLS.</span>
  232. Config *<a href="../index.html">http</a>.<a href="../index.html#Server">Server</a>
  233. <span class="comment">// contains filtered or unexported fields</span>
  234. }</pre>
  235. <p>
  236. A Server is an HTTP server listening on a system-chosen port on the
  237. local loopback interface, for use in end-to-end HTTP tests.
  238. </p>
  239. <div id="example_Server" class="toggle">
  240. <div class="collapsed">
  241. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  242. </div>
  243. <div class="expanded">
  244. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  245. <p>Code:</p>
  246. <pre class="code">ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  247. fmt.Fprintln(w, &#34;Hello, client&#34;)
  248. }))
  249. defer ts.Close()
  250. res, err := http.Get(ts.URL)
  251. if err != nil {
  252. log.Fatal(err)
  253. }
  254. greeting, err := ioutil.ReadAll(res.Body)
  255. res.Body.Close()
  256. if err != nil {
  257. log.Fatal(err)
  258. }
  259. fmt.Printf(&#34;%s&#34;, greeting)
  260. <span class="comment"></pre>
  261. <p>Output:</p>
  262. <pre class="output">Hello, client
  263. </pre>
  264. </div>
  265. </div>
  266. <h3 id="NewServer">func <a href="http://localhost:6060/src/net/http/httptest/server.go?s=2049:2093#L63">NewServer</a></h3>
  267. <pre>func NewServer(handler <a href="../index.html">http</a>.<a href="../index.html#Handler">Handler</a>) *<a href="index.html#Server">Server</a></pre>
  268. <p>
  269. NewServer starts and returns a new Server.
  270. The caller should call Close when finished, to shut it down.
  271. </p>
  272. <h3 id="NewTLSServer">func <a href="http://localhost:6060/src/net/http/httptest/server.go?s=3640:3687#L125">NewTLSServer</a></h3>
  273. <pre>func NewTLSServer(handler <a href="../index.html">http</a>.<a href="../index.html#Handler">Handler</a>) *<a href="index.html#Server">Server</a></pre>
  274. <p>
  275. NewTLSServer starts and returns a new Server using TLS.
  276. The caller should call Close when finished, to shut it down.
  277. </p>
  278. <h3 id="NewUnstartedServer">func <a href="http://localhost:6060/src/net/http/httptest/server.go?s=2374:2427#L75">NewUnstartedServer</a></h3>
  279. <pre>func NewUnstartedServer(handler <a href="../index.html">http</a>.<a href="../index.html#Handler">Handler</a>) *<a href="index.html#Server">Server</a></pre>
  280. <p>
  281. NewUnstartedServer returns a new Server but doesn&#39;t start it.
  282. </p>
  283. <p>
  284. After changing its configuration, the caller should call Start or
  285. StartTLS.
  286. </p>
  287. <p>
  288. The caller should call Close when finished, to shut it down.
  289. </p>
  290. <h3 id="Server.Close">func (*Server) <a href="http://localhost:6060/src/net/http/httptest/server.go?s=3924:3948#L137">Close</a></h3>
  291. <pre>func (s *<a href="index.html#Server">Server</a>) Close()</pre>
  292. <p>
  293. Close shuts down the server and blocks until all outstanding
  294. requests on this server have completed.
  295. </p>
  296. <h3 id="Server.CloseClientConnections">func (*Server) <a href="http://localhost:6060/src/net/http/httptest/server.go?s=6004:6045#L194">CloseClientConnections</a></h3>
  297. <pre>func (s *<a href="index.html#Server">Server</a>) CloseClientConnections()</pre>
  298. <p>
  299. CloseClientConnections closes any open HTTP connections to the test Server.
  300. </p>
  301. <h3 id="Server.Start">func (*Server) <a href="http://localhost:6060/src/net/http/httptest/server.go?s=2579:2603#L83">Start</a></h3>
  302. <pre>func (s *<a href="index.html#Server">Server</a>) Start()</pre>
  303. <p>
  304. Start starts a server from NewUnstartedServer.
  305. </p>
  306. <h3 id="Server.StartTLS">func (*Server) <a href="http://localhost:6060/src/net/http/httptest/server.go?s=2886:2913#L97">StartTLS</a></h3>
  307. <pre>func (s *<a href="index.html#Server">Server</a>) StartTLS()</pre>
  308. <p>
  309. StartTLS starts TLS on a server from NewUnstartedServer.
  310. </p>
  311. <div id="footer">
  312. Build version go1.6.<br>
  313. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  314. the content of this page is licensed under the
  315. Creative Commons Attribution 3.0 License,
  316. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  317. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  318. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  319. </div>
  320. </div><!-- .container -->
  321. </div><!-- #page -->
  322. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  323. <script type="text/javascript" src="../../../../lib/godoc/jquery.js"></script>
  324. <script type="text/javascript" src="../../../../lib/godoc/jquery.treeview.js"></script>
  325. <script type="text/javascript" src="../../../../lib/godoc/jquery.treeview.edit.js"></script>
  326. <script type="text/javascript" src="../../../../lib/godoc/godocs.js"></script>
  327. </body>
  328. </html>