an http based roguelike rpg about jelqing
| 1 | use super::*; |
| 2 | |
| 3 | #[tokio::test] |
| 4 | async fn creating_a_run_returns_location_and_etag() { |
| 5 | let response = send(&app(), "POST", "/runs?seed=1", &[], "").await; |
| 6 | assert_eq!(response.status(), StatusCode::CREATED); |
| 7 | assert!(header(&response, "location").starts_with("/runs/")); |
| 8 | assert!(response.headers().contains_key(header::ETAG)); |
| 9 | assert_eq!(header(&response, "server"), "jelqd/0.6.9"); |
| 10 | } |
| 11 | |
| 12 | #[tokio::test] |
| 13 | async fn bodies_do_not_repeat_the_headers() { |
| 14 | let Harness { app, .. } = harness(Floor::Bedroom, SEED, |_| {}); |
| 15 | let response = send(&app, "GET", "/runs/cafebabe", &[], "").await; |
| 16 | assert!(response.headers().contains_key("x-eq")); |
| 17 | let body = json(response).await; |
| 18 | for stat in [ |
| 19 | "eq", |
| 20 | "girth", |
| 21 | "foreskin", |
| 22 | "stamina", |
| 23 | "cash", |
| 24 | "self_reported_length", |
| 25 | ] { |
| 26 | assert!(body.get(stat).is_none(), "{stat} is already a header"); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | #[tokio::test] |
| 31 | async fn jelqing_cold_goes_through_at_a_price() { |
| 32 | let friction = async |warm: bool| { |
| 33 | let app = seeded_with(Floor::Bedroom, |run| { |
| 34 | run.body.warmed_up = warm; |
| 35 | run.body.foreskin = Some(1.0); |
| 36 | }); |
| 37 | let response = send(&app, "JELQ", "/runs/cafebabe?technique=stretch", &[], "").await; |
| 38 | let noted = warnings(&response) |
| 39 | .iter() |
| 40 | .any(|w| w.contains("Warm-up was skipped")); |
| 41 | assert_eq!(noted, !warm); |
| 42 | header(&response, "x-friction").to_owned() |
| 43 | }; |
| 44 | assert_eq!(friction(true).await, "1/10"); |
| 45 | assert_eq!(friction(false).await, "3/10"); |
| 46 | } |
| 47 | |
| 48 | #[tokio::test] |
| 49 | async fn wet_without_lube_is_unsupported_media() { |
| 50 | let Harness { app, .. } = harness(Floor::Bedroom, SEED, |_| {}); |
| 51 | send(&app, "JELQ", "/runs/cafebabe?technique=warmup", &[], "").await; |
| 52 | let response = send(&app, "JELQ", "/runs/cafebabe?technique=wet", &[], "").await; |
| 53 | assert_eq!(response.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE); |
| 54 | let images = send( |
| 55 | &app, |
| 56 | "JELQ", |
| 57 | "/runs/cafebabe?technique=wet", |
| 58 | &[("content-type", "image/jpeg")], |
| 59 | "", |
| 60 | ) |
| 61 | .await; |
| 62 | assert_eq!(json(images).await["detail"], "No."); |
| 63 | } |
| 64 | |
| 65 | #[tokio::test] |
| 66 | async fn unknown_methods_are_not_implemented() { |
| 67 | let Harness { app, .. } = harness(Floor::Bedroom, SEED, |_| {}); |
| 68 | let response = send(&app, "MILK", "/runs/cafebabe", &[], "").await; |
| 69 | assert_eq!(response.status(), StatusCode::NOT_IMPLEMENTED); |
| 70 | let teapot = send(&app, "BREW", "/runs/cafebabe", &[], "").await; |
| 71 | assert_eq!(teapot.status(), StatusCode::IM_A_TEAPOT); |
| 72 | } |
| 73 | |
| 74 | #[tokio::test] |
| 75 | async fn the_clinic_does_not_advertise_jelq() { |
| 76 | let Harness { app, .. } = harness(Floor::Clinic, SEED, |_| {}); |
| 77 | let options = send(&app, "OPTIONS", "/runs/cafebabe", &[], "").await; |
| 78 | assert!(!header(&options, "allow").contains("JELQ")); |
| 79 | let jelq = send(&app, "JELQ", "/runs/cafebabe?technique=wet", &[], "").await; |
| 80 | assert_eq!(jelq.status(), StatusCode::METHOD_NOT_ALLOWED); |
| 81 | assert!(!header(&jelq, "allow").contains("JELQ")); |
| 82 | let Harness { app: bedroom, .. } = harness(Floor::Bedroom, SEED, |_| {}); |
| 83 | let options = send(&bedroom, "OPTIONS", "/runs/cafebabe", &[], "").await; |
| 84 | assert!(header(&options, "allow").contains("JELQ")); |
| 85 | } |
| 86 | |
| 87 | #[tokio::test] |
| 88 | async fn patch_only_accepts_the_truth() { |
| 89 | let Harness { app, .. } = harness(Floor::Bedroom, SEED, |_| {}); |
| 90 | let patch = [("content-type", "application/json-patch+json")]; |
| 91 | let replace = send( |
| 92 | &app, |
| 93 | "PATCH", |
| 94 | "/runs/cafebabe", |
| 95 | &patch, |
| 96 | r#"[{"op":"replace","path":"/length","value":12}]"#, |
| 97 | ) |
| 98 | .await; |
| 99 | assert_eq!(replace.status(), StatusCode::UNPROCESSABLE_ENTITY); |
| 100 | let lie = send( |
| 101 | &app, |
| 102 | "PATCH", |
| 103 | "/runs/cafebabe", |
| 104 | &patch, |
| 105 | r#"[{"op":"test","path":"/length","value":40}]"#, |
| 106 | ) |
| 107 | .await; |
| 108 | assert_eq!( |
| 109 | json(lie).await["type"], |
| 110 | "https://jelq.dev/problems/exaggeration" |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | #[tokio::test] |
| 115 | async fn http_1_0_gets_the_pdf_treatment() { |
| 116 | let request = Request::builder() |
| 117 | .uri("/graveyard") |
| 118 | .version(Version::HTTP_10) |
| 119 | .body(Body::empty()) |
| 120 | .unwrap(); |
| 121 | let response = app().oneshot(request).await.unwrap(); |
| 122 | assert_eq!(response.status(), StatusCode::HTTP_VERSION_NOT_SUPPORTED); |
| 123 | } |
| 124 | |
| 125 | #[tokio::test] |
| 126 | async fn links_offer_only_usable_techniques() { |
| 127 | let app = seeded_with(Floor::Bedroom, |run| run.body.foreskin = Some(1.0)); |
| 128 | let cold = send(&app, "GET", "/runs/cafebabe", &[], "").await; |
| 129 | assert!( |
| 130 | advertised(&cold).contains(&"stretch".to_owned()), |
| 131 | "a cold jelq is allowed" |
| 132 | ); |
| 133 | let warm = send(&app, "JELQ", "/runs/cafebabe?technique=warmup", &[], "").await; |
| 134 | let usable = advertised(&warm); |
| 135 | assert!(usable.contains(&"wet".to_owned()) && usable.contains(&"dry".to_owned())); |
| 136 | assert!(!usable.contains(&"uli".to_owned()), "uli needs the forum"); |
| 137 | assert!(!usable.contains(&"pump".to_owned()), "pump needs a pump"); |
| 138 | assert!( |
| 139 | header(&warm, "link").contains("title=\"wet\"; allow=\"JELQ\"; lubricant=\"required\"") |
| 140 | ); |
| 141 | |
| 142 | let Harness { app: forum, .. } = harness(Floor::Forum, SEED, |_| {}); |
| 143 | let response = send(&forum, "GET", "/runs/cafebabe", &[], "").await; |
| 144 | assert!(advertised(&response).is_empty()); |
| 145 | } |
| 146 | |
| 147 | #[tokio::test] |
| 148 | async fn links_hide_resources_that_would_do_nothing() { |
| 149 | let Harness { app: bedroom, .. } = harness(Floor::Bedroom, SEED, |_| {}); |
| 150 | let before = linked_rels(&send(&bedroom, "GET", "/runs/cafebabe", &[], "").await); |
| 151 | assert!(before.contains(&"drawer".to_owned())); |
| 152 | assert!(!before.contains(&"diagnosis".to_owned())); |
| 153 | assert!(!before.contains(&"thread".to_owned())); |
| 154 | let searched = send(&bedroom, "POST", "/runs/cafebabe/drawer", &[], "").await; |
| 155 | assert!(!linked_rels(&searched).contains(&"drawer".to_owned())); |
| 156 | |
| 157 | let Harness { app: clinic, .. } = harness(Floor::Clinic, SEED, |_| {}); |
| 158 | let rels = linked_rels(&send(&clinic, "GET", "/runs/cafebabe", &[], "").await); |
| 159 | assert!(rels.contains(&"diagnosis".to_owned()) && rels.contains(&"thread".to_owned())); |
| 160 | } |