Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LocalResponse, cookies, and index testing #948

Closed
sjsadowski opened this issue Mar 13, 2019 · 2 comments
Closed

LocalResponse, cookies, and index testing #948

sjsadowski opened this issue Mar 13, 2019 · 2 comments

Comments

@sjsadowski
Copy link

rust: 1.35.0-nightly (7c19e1eed 2019-03-12)
rocket: 0.4.0

I'm trying to advance my tests before I implement, following a standard TDD pattern and I'm running in to a problem and it's driving me batty.

Also: this is my first week working with rust so I know I'm doing something wrong, I just don't know what it is.

I'm trying to simply test for the presence of a cookie; at this stage I don't care that there's anything in the cookie.

My test:

#[test]
fn invalid_post_auth() {
    let client = Client::new(rocket()).expect("valid rocket instance");
    let mut response =
        client.post("/auth")
            .body("username=tester&password=testpass")
            .header(ContentType::Form)
            .dispatch();

    assert_eq!(response.cookies().get("Auth-Token").is_none(), true);
    assert_eq!(response.status(), Status::Unauthorized);
    assert_eq!(response.body_string(), Some(Json(json!({"status": "NOT OK"})).to_string()))
}

However, rustc (via cargo test) complains:

 error[E0277]: the type `[rocket::http::Cookie<'_>]` cannot be indexed by `&str`
  --> src/tests.rs:44:35
   |
44 |     assert_eq!(response.cookies().get("X-Auth-Token").is_none(), true);
   |                                   ^^^ slice indices are of type `usize` or ranges of `usize`
   |
   = help: the trait `std::slice::SliceIndex<[rocket::http::Cookie<'_>]>` is not implemented for `&str`

Now based on what I've read here: https://api.rocket.rs/v0.4/rocket/local/struct.LocalResponse.html#method.cookies and here: https://api.rocket.rs/rocket/http/enum.Cookies.html - I think I should be able to pass an &str to retrieve a specific cookie and just verify it exists (or doesn't). However the help message is clearly telling me that it .get() expects a usize.

So what am I missing here? I don't need a straight answer, if you can just point me to the right place to RTFM, that's good enough.

@sjsadowski sjsadowski changed the title LocalResponse, testing, cookies, and index testing LocalResponse, cookies, and index testing Mar 13, 2019
@jebrosen
Copy link
Collaborator

response.cookies() returns a Vec<Cookie>, not a Cookies. You'll need to use something like .find(|c| c.name() == "X-Auth-Token") instead.

That is a bit unusual though, and I think incongruent with both Request and Response.

@sjsadowski
Copy link
Author

Thanks for the quick response! Maybe it should be brought in line at some point? For now though I am covered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants