34 lines
7.6 KiB
HTML
34 lines
7.6 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `read_response_json` fn in crate `actix_web`."><meta name="keywords" content="rust, rustlang, rust-lang, read_response_json"><title>actix_web::test::read_response_json - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="shortcut icon" href="../../favicon.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc fn"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../actix_web/index.html'><div class='logo-container'><img src='../../rust-logo.png' alt='logo'></div></a><div class="sidebar-elems"><p class='location'><a href='../index.html'>actix_web</a>::<wbr><a href='index.html'>test</a></p><script>window.sidebarCurrent = {name: 'read_response_json', ty: 'fn', relpath: ''};</script><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../../src/actix_web/test.rs.html#233-254' title='goto source code'>[src]</a></span><span class='in-band'>Function <a href='../index.html'>actix_web</a>::<wbr><a href='index.html'>test</a>::<wbr><a class="fn" href=''>read_response_json</a></span></h1><pre class='rust fn'>pub fn read_response_json<S, B, T>(app: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&mut </a>S, req: <a class="struct" href="../../actix_http/request/struct.Request.html" title="struct actix_http::request::Request">Request</a>) -> T <span class="where fmt-newline">where<br> S: <a class="trait" href="../../actix_web/dev/trait.Service.html" title="trait actix_web::dev::Service">Service</a><Request = <a class="struct" href="../../actix_http/request/struct.Request.html" title="struct actix_http::request::Request">Request</a>, Response = <a class="struct" href="../../actix_web/dev/struct.ServiceResponse.html" title="struct actix_web::dev::ServiceResponse">ServiceResponse</a><B>, Error = <a class="struct" href="../../actix_web/error/struct.Error.html" title="struct actix_web::error::Error">Error</a>>,<br> B: <a class="trait" href="../../actix_web/dev/trait.MessageBody.html" title="trait actix_web::dev::MessageBody">MessageBody</a>,<br> T: <a class="trait" href="../../serde/de/trait.DeserializeOwned.html" title="trait serde::de::DeserializeOwned">DeserializeOwned</a>, </span></pre><div class='docblock'><p>Helper function that returns a deserialized response body of a TestRequest
|
||
This function blocks the current thread until futures complete.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">actix_web</span>::{<span class="ident">App</span>, <span class="ident">test</span>, <span class="ident">web</span>, <span class="ident">HttpResponse</span>, <span class="ident">http</span>::<span class="ident">header</span>};
|
||
<span class="kw">use</span> <span class="ident">serde</span>::{<span class="ident">Serialize</span>, <span class="ident">Deserialize</span>};
|
||
|
||
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Serialize</span>, <span class="ident">Deserialize</span>)]</span>
|
||
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">Person</span> {
|
||
<span class="ident">id</span>: <span class="ident">String</span>,
|
||
<span class="ident">name</span>: <span class="ident">String</span>
|
||
}
|
||
|
||
<span class="attribute">#[<span class="ident">test</span>]</span>
|
||
<span class="kw">fn</span> <span class="ident">test_add_person</span>() {
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">app</span> <span class="op">=</span> <span class="ident">test</span>::<span class="ident">init_service</span>(
|
||
<span class="ident">App</span>::<span class="ident">new</span>().<span class="ident">service</span>(
|
||
<span class="ident">web</span>::<span class="ident">resource</span>(<span class="string">"/people"</span>)
|
||
.<span class="ident">route</span>(<span class="ident">web</span>::<span class="ident">post</span>().<span class="ident">to</span>(<span class="op">|</span><span class="ident">person</span>: <span class="ident">web</span>::<span class="ident">Json</span><span class="op"><</span><span class="ident">Person</span><span class="op">></span><span class="op">|</span> {
|
||
<span class="ident">HttpResponse</span>::<span class="prelude-val">Ok</span>()
|
||
.<span class="ident">json</span>(<span class="ident">person</span>.<span class="ident">into_inner</span>())})
|
||
)));
|
||
|
||
<span class="kw">let</span> <span class="ident">payload</span> <span class="op">=</span> <span class="string">r#"{"id":"12345","name":"User name"}"#</span>.<span class="ident">as_bytes</span>();
|
||
|
||
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">test</span>::<span class="ident">TestRequest</span>::<span class="ident">post</span>()
|
||
.<span class="ident">uri</span>(<span class="string">"/people"</span>)
|
||
.<span class="ident">header</span>(<span class="ident">header</span>::<span class="ident">CONTENT_TYPE</span>, <span class="string">"application/json"</span>)
|
||
.<span class="ident">set_payload</span>(<span class="ident">payload</span>)
|
||
.<span class="ident">to_request</span>();
|
||
|
||
<span class="kw">let</span> <span class="ident">result</span>: <span class="ident">Person</span> <span class="op">=</span> <span class="ident">test</span>::<span class="ident">read_response_json</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">app</span>, <span class="ident">req</span>);
|
||
}</pre></div>
|
||
</div></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../../";window.currentCrate = "actix_web";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html> |