playwright/api/
response.rs

1use crate::{
2    api::{Frame, Request},
3    imp::{core::*, prelude::*, response::Response as Impl, utils::Header}
4};
5
6#[derive(Debug, Clone)]
7pub struct Response {
8    inner: Weak<Impl>
9}
10
11impl PartialEq for Response {
12    fn eq(&self, other: &Self) -> bool {
13        let a = self.inner.upgrade();
14        let b = other.inner.upgrade();
15        a.and_then(|a| b.map(|b| (a, b)))
16            .map(|(a, b)| a.guid() == b.guid())
17            .unwrap_or_default()
18    }
19}
20
21impl Response {
22    pub(crate) fn new(inner: Weak<Impl>) -> Self { Self { inner } }
23
24    pub fn url(&self) -> Result<String, Error> { Ok(upgrade(&self.inner)?.url().into()) }
25    /// Contains the status code of the response (e.g., 200 for a success).
26    pub fn status(&self) -> Result<i32, Error> { Ok(upgrade(&self.inner)?.status()) }
27    /// Contains the status text of the response (e.g. usually an "OK" for a success).
28    pub fn status_text(&self) -> Result<String, Error> {
29        Ok(upgrade(&self.inner)?.status_text().into())
30    }
31
32    /// Contains a boolean stating whether the response was successful (status in the range 200-299) or not.
33    pub fn ok(&self) -> Result<bool, Error> { Ok(upgrade(&self.inner)?.ok()) }
34
35    pub fn request(&self) -> Request {
36        let inner = weak_and_then(&self.inner, |rc| rc.request());
37        Request::new(inner)
38    }
39
40    /// Waits for this response to finish, returns failure error if request failed.
41    pub async fn finished(&self) -> ArcResult<Option<String>> {
42        upgrade(&self.inner)?.finished().await
43    }
44
45    pub async fn body(&self) -> ArcResult<Vec<u8>> { upgrade(&self.inner)?.body().await }
46
47    /// Returns the text representation of response body.
48    pub async fn text(&self) -> ArcResult<String> { upgrade(&self.inner)?.text().await }
49
50    /// Returns the object with HTTP headers associated with the response. All header names are lower-case.
51    pub async fn headers(&self) -> ArcResult<Vec<Header>> { upgrade(&self.inner)?.headers().await }
52
53    /// Shortcut for [`Response::request`]'s  [`Request::frame`]
54    pub fn frame(&self) -> Frame { self.request().frame() }
55}