mirror of
https://github.com/actix/actix-extras.git
synced 2025-08-30 11:08:08 +02:00
Deploying to gh-pages from @ ce92f0036f
🚀
This commit is contained in:
@@ -360,6 +360,61 @@
|
||||
<span id="355">355</span>
|
||||
<span id="356">356</span>
|
||||
<span id="357">357</span>
|
||||
<span id="358">358</span>
|
||||
<span id="359">359</span>
|
||||
<span id="360">360</span>
|
||||
<span id="361">361</span>
|
||||
<span id="362">362</span>
|
||||
<span id="363">363</span>
|
||||
<span id="364">364</span>
|
||||
<span id="365">365</span>
|
||||
<span id="366">366</span>
|
||||
<span id="367">367</span>
|
||||
<span id="368">368</span>
|
||||
<span id="369">369</span>
|
||||
<span id="370">370</span>
|
||||
<span id="371">371</span>
|
||||
<span id="372">372</span>
|
||||
<span id="373">373</span>
|
||||
<span id="374">374</span>
|
||||
<span id="375">375</span>
|
||||
<span id="376">376</span>
|
||||
<span id="377">377</span>
|
||||
<span id="378">378</span>
|
||||
<span id="379">379</span>
|
||||
<span id="380">380</span>
|
||||
<span id="381">381</span>
|
||||
<span id="382">382</span>
|
||||
<span id="383">383</span>
|
||||
<span id="384">384</span>
|
||||
<span id="385">385</span>
|
||||
<span id="386">386</span>
|
||||
<span id="387">387</span>
|
||||
<span id="388">388</span>
|
||||
<span id="389">389</span>
|
||||
<span id="390">390</span>
|
||||
<span id="391">391</span>
|
||||
<span id="392">392</span>
|
||||
<span id="393">393</span>
|
||||
<span id="394">394</span>
|
||||
<span id="395">395</span>
|
||||
<span id="396">396</span>
|
||||
<span id="397">397</span>
|
||||
<span id="398">398</span>
|
||||
<span id="399">399</span>
|
||||
<span id="400">400</span>
|
||||
<span id="401">401</span>
|
||||
<span id="402">402</span>
|
||||
<span id="403">403</span>
|
||||
<span id="404">404</span>
|
||||
<span id="405">405</span>
|
||||
<span id="406">406</span>
|
||||
<span id="407">407</span>
|
||||
<span id="408">408</span>
|
||||
<span id="409">409</span>
|
||||
<span id="410">410</span>
|
||||
<span id="411">411</span>
|
||||
<span id="412">412</span>
|
||||
</pre><pre class="rust"><code><span class="doccomment">//! HTTP Authentication middleware.</span>
|
||||
|
||||
<span class="kw">use</span> <span class="ident">std</span>::{
|
||||
@@ -602,10 +657,13 @@
|
||||
<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">test</span>)]</span>
|
||||
<span class="kw">mod</span> <span class="ident">tests</span> {
|
||||
<span class="kw">use</span> <span class="kw">super</span>::<span class="kw-2">*</span>;
|
||||
<span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::extractors::basic::BasicAuth</span>;
|
||||
<span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::extractors::bearer::BearerAuth</span>;
|
||||
<span class="kw">use</span> <span class="ident">actix_service</span>::{<span class="ident">into_service</span>, <span class="ident">Service</span>};
|
||||
<span class="kw">use</span> <span class="ident">actix_web::error::ErrorForbidden</span>;
|
||||
<span class="kw">use</span> <span class="ident">actix_web::http::StatusCode</span>;
|
||||
<span class="kw">use</span> <span class="ident">actix_web::test::TestRequest</span>;
|
||||
<span class="kw">use</span> <span class="ident">actix_web</span>::{<span class="ident">error</span>, <span class="ident">HttpResponse</span>};
|
||||
<span class="kw">use</span> <span class="ident">actix_web</span>::{<span class="ident">error</span>, <span class="ident">web</span>, <span class="ident">App</span>, <span class="ident">HttpResponse</span>};
|
||||
|
||||
<span class="doccomment">/// This is a test for https://github.com/actix/actix-extras/issues/10</span>
|
||||
<span class="attribute">#[<span class="ident">actix_web::test</span>]</span>
|
||||
@@ -716,6 +774,58 @@
|
||||
|
||||
<span class="macro">assert!</span>(<span class="ident">f</span>.<span class="ident">is_ok</span>());
|
||||
}
|
||||
|
||||
<span class="attribute">#[<span class="ident">actix_rt::test</span>]</span>
|
||||
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">test_middleware_works_with_app</span>() {
|
||||
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">validator</span>(
|
||||
<span class="ident">_req</span>: <span class="ident">ServiceRequest</span>,
|
||||
<span class="ident">_credentials</span>: <span class="ident">BasicAuth</span>,
|
||||
) -> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">ServiceRequest</span>, <span class="ident">actix_web::Error</span><span class="op">></span> {
|
||||
<span class="prelude-val">Err</span>(<span class="ident">ErrorForbidden</span>(<span class="string">"You are not welcome!"</span>))
|
||||
}
|
||||
<span class="kw">let</span> <span class="ident">middleware</span> <span class="op">=</span> <span class="ident">HttpAuthentication::basic</span>(<span class="ident">validator</span>);
|
||||
|
||||
<span class="kw">let</span> <span class="ident">srv</span> <span class="op">=</span> <span class="ident">actix_web::test::init_service</span>(
|
||||
<span class="ident">App::new</span>()
|
||||
.<span class="ident">wrap</span>(<span class="ident">middleware</span>)
|
||||
.<span class="ident">route</span>(<span class="string">"/"</span>, <span class="ident">web::get</span>().<span class="ident">to</span>(<span class="op">|</span><span class="op">|</span> <span class="ident">web::HttpResponse::Ok</span>())),
|
||||
)
|
||||
.<span class="kw">await</span>;
|
||||
|
||||
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">actix_web::test::TestRequest::with_uri</span>(<span class="string">"/"</span>)
|
||||
.<span class="ident">append_header</span>((<span class="string">"Authorization"</span>, <span class="string">"Basic DontCare"</span>))
|
||||
.<span class="ident">to_request</span>();
|
||||
|
||||
<span class="kw">let</span> <span class="ident">resp</span> <span class="op">=</span> <span class="ident">srv</span>.<span class="ident">call</span>(<span class="ident">req</span>).<span class="kw">await</span>.<span class="ident">unwrap</span>();
|
||||
<span class="macro">assert_eq!</span>(<span class="ident">resp</span>.<span class="ident">status</span>(), <span class="ident">StatusCode::UNAUTHORIZED</span>);
|
||||
}
|
||||
|
||||
<span class="attribute">#[<span class="ident">actix_rt::test</span>]</span>
|
||||
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">test_middleware_works_with_scope</span>() {
|
||||
<span class="kw">async</span> <span class="kw">fn</span> <span class="ident">validator</span>(
|
||||
<span class="ident">_req</span>: <span class="ident">ServiceRequest</span>,
|
||||
<span class="ident">_credentials</span>: <span class="ident">BasicAuth</span>,
|
||||
) -> <span class="prelude-ty">Result</span><span class="op"><</span><span class="ident">ServiceRequest</span>, <span class="ident">actix_web::Error</span><span class="op">></span> {
|
||||
<span class="prelude-val">Err</span>(<span class="ident">ErrorForbidden</span>(<span class="string">"You are not welcome!"</span>))
|
||||
}
|
||||
<span class="kw">let</span> <span class="ident">middleware</span> <span class="op">=</span> <span class="ident">actix_web::middleware::Compat::new</span>(<span class="ident">HttpAuthentication::basic</span>(<span class="ident">validator</span>));
|
||||
|
||||
<span class="kw">let</span> <span class="ident">srv</span> <span class="op">=</span> <span class="ident">actix_web::test::init_service</span>(
|
||||
<span class="ident">App::new</span>().<span class="ident">service</span>(
|
||||
<span class="ident">web::scope</span>(<span class="string">"/"</span>)
|
||||
.<span class="ident">wrap</span>(<span class="ident">middleware</span>)
|
||||
.<span class="ident">route</span>(<span class="string">"/"</span>, <span class="ident">web::get</span>().<span class="ident">to</span>(<span class="op">|</span><span class="op">|</span> <span class="ident">web::HttpResponse::Ok</span>())),
|
||||
),
|
||||
)
|
||||
.<span class="kw">await</span>;
|
||||
|
||||
<span class="kw">let</span> <span class="ident">req</span> <span class="op">=</span> <span class="ident">actix_web::test::TestRequest::with_uri</span>(<span class="string">"/"</span>)
|
||||
.<span class="ident">append_header</span>((<span class="string">"Authorization"</span>, <span class="string">"Basic DontCare"</span>))
|
||||
.<span class="ident">to_request</span>();
|
||||
|
||||
<span class="kw">let</span> <span class="ident">resp</span> <span class="op">=</span> <span class="ident">srv</span>.<span class="ident">call</span>(<span class="ident">req</span>).<span class="kw">await</span>.<span class="ident">unwrap</span>();
|
||||
<span class="macro">assert_eq!</span>(<span class="ident">resp</span>.<span class="ident">status</span>(), <span class="ident">StatusCode::UNAUTHORIZED</span>);
|
||||
}
|
||||
}
|
||||
</code></pre></div>
|
||||
</section><section id="search" class="content hidden"></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="actix_web_httpauth" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.61.0-nightly (4ce374923 2022-02-28)" ></div>
|
||||
|
Reference in New Issue
Block a user