mirror of
https://github.com/actix/actix-website
synced 2025-06-29 16:24:58 +02:00
update testing to actix-web 2.0.0 and improve stream testing code (#141)
* update testing to actix-web 2.0.0 and improve stream testing code
This commit is contained in:
committed by
Yuki Okushi
parent
13f0756d33
commit
bc4e0422c8
@ -11,10 +11,9 @@ integration tests.
|
||||
|
||||
# Unit Tests
|
||||
|
||||
For unit testing, actix-web provides a request builder type and a simple handler runner.
|
||||
[*TestRequest*][testrequest] implements a builder-like pattern. You can generate a
|
||||
`HttpRequest` instance with `to_http_request()`, or you can
|
||||
run your handler with `block_on()`.
|
||||
For unit testing, actix-web provides a request builder type.
|
||||
[*TestRequest*][testrequest] implements a builder-like pattern. You can generate a
|
||||
`HttpRequest` instance with `to_http_request()` and call your handler with it.
|
||||
|
||||
{{< include-example example="testing" file="main.rs" section="unit-tests" >}}
|
||||
|
||||
@ -23,7 +22,7 @@ run your handler with `block_on()`.
|
||||
There a few methods for testing your application. Actix-web can be used
|
||||
to run the application with specific handlers in a real http server.
|
||||
|
||||
`TestRequest::get()`, `TestRequest::post()`, and `TestRequest::client()`
|
||||
`TestRequest::get()`, `TestRequest::post()` and other
|
||||
methods can be used to send requests to the test server.
|
||||
|
||||
To create a `Service` for testing, use the `test::init_service` method which accepts a
|
||||
@ -33,15 +32,6 @@ regular `App` builder.
|
||||
|
||||
{{< include-example example="testing" file="integration_one.rs" section="integration-one" >}}
|
||||
|
||||
Note: If you get the error message "no Task is currently running" when running your
|
||||
test, you may need to wrap your service call in
|
||||
[test::run_on](https://docs.rs/actix-web/1/actix_web/test/fn.run_on.html):
|
||||
|
||||
```rust
|
||||
let future = test::run_on(|| app.call(req));
|
||||
let resp = test::block_on(future).unwrap();
|
||||
```
|
||||
|
||||
If you need more complex application configuration testing should be very similar to creating
|
||||
the normal application. For example, you may need to initialize application state. Create an
|
||||
`App` with a `data` method and attach state just like you would from a normal application.
|
||||
@ -50,13 +40,13 @@ the normal application. For example, you may need to initialize application stat
|
||||
|
||||
# Stream response tests
|
||||
|
||||
If you need to test stream it would be enough to convert a [*ClientResponse*][clientresponse]
|
||||
If you need to test stream it would be enough to call `take_body()` and convert a resulting [*ResponseBody*][responsebody]
|
||||
to future and execute it.
|
||||
For example of testing [*Server Sent Events*][serversentevents].
|
||||
|
||||
{{< include-example example="testing" file="stream_response.rs" section="stream-response" >}}
|
||||
|
||||
[serversentevents]: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
|
||||
[clientresponse]: https://docs.rs/actix-web/1/actix_web/client/struct.ClientResponse.html
|
||||
[actixdocs]: https://docs.rs/actix-web/1/actix_web/test/index.html
|
||||
[testrequest]: https://docs.rs/actix-web/1/actix_web/error/trait.ResponseError.html#foreign-impls
|
||||
[responsebody]: https://docs.rs/actix-web/2/actix_web/body/enum.ResponseBody.html
|
||||
[actixdocs]: https://docs.rs/actix-web/2/actix_web/test/index.html
|
||||
[testrequest]: https://docs.rs/actix-web/2/actix_web/error/trait.ResponseError.html#foreign-impls
|
||||
|
Reference in New Issue
Block a user