Skip to content

Commit fe023bc

Browse files
committed
chore: add tests for the new behaviour
1 parent 0baedf9 commit fe023bc

6 files changed

Lines changed: 163 additions & 50 deletions

File tree

syncserver/src/server/test.rs

Lines changed: 126 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,23 @@ macro_rules! init_app {
147147
}
148148

149149
fn create_request(
150-
method: http::Method,
150+
method: &http::Method,
151151
path: &str,
152152
headers: Option<HashMap<&'static str, String>>,
153153
payload: Option<serde_json::Value>,
154+
settings: Option<&Settings>,
154155
) -> test::TestRequest {
155-
let settings = get_test_settings();
156+
let test_settings = get_test_settings();
157+
let settings = settings.unwrap_or(&test_settings);
156158
let mut req = test::TestRequest::with_uri(path)
157159
.method(method.clone())
158160
.insert_header((
159161
"Authorization",
160-
create_hawk_header(method.as_str(), settings.port, path),
162+
create_hawk_header(
163+
method.as_str(),
164+
settings.port,
165+
&(ReverseProxyState::from_settings(settings).get_webroot() + path),
166+
),
161167
))
162168
.insert_header(("Accept", "application/json"))
163169
.insert_header((
@@ -229,20 +235,34 @@ async fn test_endpoint(
229235
status: Option<StatusCode>,
230236
expected_body: Option<&str>,
231237
) {
232-
let app = init_app!().await;
233-
234-
let req = create_request(method, path, None, None).to_request();
235-
let sresp = app
236-
.call(req)
237-
.await
238-
.expect("Could not get sresp in test_endpoint");
239-
match status {
240-
None => assert!(sresp.response().status().is_success()),
241-
Some(status) => assert!(sresp.response().status() == status),
242-
};
243-
if let Some(x_body) = expected_body {
244-
let body = test::read_body(sresp).await;
245-
assert_eq!(body, x_body.as_bytes());
238+
for prefix in [None, Some("/firefox-sync")] {
239+
let settings = match prefix {
240+
None => None,
241+
Some(prefix) => {
242+
let mut settings = get_test_settings();
243+
settings.public_url = Some(format!("https://example.com{}", prefix).to_owned());
244+
Some(settings)
245+
}
246+
};
247+
248+
let app = match settings.clone() {
249+
None => init_app!().await,
250+
Some(settings) => init_app!(settings).await,
251+
};
252+
253+
let req = create_request(&method, path, None, None, settings.as_ref()).to_request();
254+
let sresp = app
255+
.call(req)
256+
.await
257+
.expect("Could not get sresp in test_endpoint");
258+
match status {
259+
None => assert!(sresp.response().status().is_success()),
260+
Some(status) => assert!(sresp.response().status() == status),
261+
};
262+
if let Some(x_body) = expected_body {
263+
let body = test::read_body(sresp).await;
264+
assert_eq!(body, x_body.as_bytes());
265+
}
246266
}
247267
}
248268

@@ -265,7 +285,7 @@ where
265285
))
266286
.await;
267287

268-
let req = create_request(method, path, None, None).to_request();
288+
let req = create_request(&method, path, None, None, Some(&settings)).to_request();
269289
let sresponse = match app.call(req).await {
270290
Ok(v) => v,
271291
Err(e) => {
@@ -309,11 +329,12 @@ async fn test_endpoint_with_body(
309329
metrics
310330
))
311331
.await;
312-
let req = create_request(method, path, None, Some(body)).to_request();
332+
let req = create_request(&method, path, None, Some(body), Some(&settings)).to_request();
313333
let sresponse = app
314334
.call(req)
315335
.await
316336
.expect("Could not get sresponse in test_endpoint_with_body");
337+
println!("{sresponse:#?}");
317338
assert!(sresponse.response().status().is_success());
318339
test::read_body(sresponse).await
319340
}
@@ -530,7 +551,7 @@ async fn invalid_content_type() {
530551
let mut headers = HashMap::new();
531552
headers.insert("Content-Type", "application/javascript".to_owned());
532553
let req = create_request(
533-
http::Method::PUT,
554+
&http::Method::PUT,
534555
path,
535556
Some(headers.clone()),
536557
Some(json!(BsoBody {
@@ -540,6 +561,7 @@ async fn invalid_content_type() {
540561
ttl: Some(31_536_000),
541562
..Default::default()
542563
})),
564+
None,
543565
)
544566
.to_request();
545567

@@ -553,7 +575,7 @@ async fn invalid_content_type() {
553575
let path = "/1.5/42/storage/bookmarks";
554576

555577
let req = create_request(
556-
http::Method::POST,
578+
&http::Method::POST,
557579
path,
558580
Some(headers.clone()),
559581
Some(json!([BsoBody {
@@ -563,6 +585,7 @@ async fn invalid_content_type() {
563585
ttl: Some(31_536_000),
564586
..Default::default()
565587
}])),
588+
None,
566589
)
567590
.to_request();
568591

@@ -580,13 +603,14 @@ async fn invalid_batch_post() {
580603
let mut headers = HashMap::new();
581604
headers.insert("accept", "application/json".to_owned());
582605
let req = create_request(
583-
http::Method::POST,
606+
&http::Method::POST,
584607
"/1.5/42/storage/tabs?batch=sammich",
585608
Some(headers),
586609
Some(json!([
587610
{"id": "123", "payload": "xxx", "sortindex": 23},
588611
{"id": "456", "payload": "xxxasdf", "sortindex": 23}
589612
])),
613+
None,
590614
)
591615
.to_request();
592616

@@ -610,10 +634,11 @@ async fn accept_new_or_dev_ios() {
610634
);
611635

612636
let req = create_request(
613-
http::Method::GET,
637+
&http::Method::GET,
614638
"/1.5/42/info/collections",
615639
Some(headers),
616640
None,
641+
None,
617642
)
618643
.to_request();
619644
let response = app.call(req).await.unwrap();
@@ -627,10 +652,11 @@ async fn accept_new_or_dev_ios() {
627652
);
628653

629654
let req = create_request(
630-
http::Method::GET,
655+
&http::Method::GET,
631656
"/1.5/42/info/collections",
632657
Some(headers),
633658
None,
659+
None,
634660
)
635661
.to_request();
636662
let response = app.call(req).await.unwrap();
@@ -644,10 +670,11 @@ async fn accept_new_or_dev_ios() {
644670
);
645671

646672
let req = create_request(
647-
http::Method::GET,
673+
&http::Method::GET,
648674
"/1.5/42/info/collections",
649675
Some(headers),
650676
None,
677+
None,
651678
)
652679
.to_request();
653680
let response = app.call(req).await.unwrap();
@@ -664,22 +691,24 @@ async fn reject_old_ios() {
664691
);
665692

666693
let req = create_request(
667-
http::Method::GET,
694+
&http::Method::GET,
668695
"/1.5/42/info/collections",
669696
Some(headers.clone()),
670697
None,
698+
None,
671699
)
672700
.to_request();
673701
let response = app.call(req).await.unwrap();
674702
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
675703

676704
let req = create_request(
677-
http::Method::POST,
705+
&http::Method::POST,
678706
"/1.5/42/storage/tabs?batch=sammich",
679707
Some(headers),
680708
Some(json!([
681709
{"id": "123", "payload": "xxx", "sortindex": 23},
682710
])),
711+
None,
683712
)
684713
.to_request();
685714
let response = app.call(req).await.unwrap();
@@ -691,8 +720,14 @@ async fn reject_old_ios() {
691720
#[actix_rt::test]
692721
async fn info_configuration_xlm() {
693722
let app = init_app!().await;
694-
let req =
695-
create_request(http::Method::GET, "/1.5/42/info/configuration", None, None).to_request();
723+
let req = create_request(
724+
&http::Method::GET,
725+
"/1.5/42/info/configuration",
726+
None,
727+
None,
728+
None,
729+
)
730+
.to_request();
696731
let response = app.call(req).await.unwrap();
697732
assert_eq!(response.status(), StatusCode::OK);
698733
let xlm = response.headers().get(X_LAST_MODIFIED);
@@ -716,18 +751,26 @@ async fn overquota() {
716751
let app = init_app!(settings).await;
717752

718753
// Clear out any data that's already in the store.
719-
let req = create_request(http::Method::DELETE, "/1.5/42/storage", None, None).to_request();
754+
let req = create_request(
755+
&http::Method::DELETE,
756+
"/1.5/42/storage",
757+
None,
758+
None,
759+
Some(&settings),
760+
)
761+
.to_request();
720762
let resp = app.call(req).await.unwrap();
721763
assert!(resp.response().status().is_success());
722764

723765
// Quota is enforced before the write, allowing one write to go over
724766
let req = create_request(
725-
http::Method::PUT,
767+
&http::Method::PUT,
726768
"/1.5/42/storage/xxx_col2/12345",
727769
None,
728770
Some(json!(
729771
{"payload": "*".repeat(500)}
730772
)),
773+
Some(&settings),
731774
)
732775
.to_request();
733776
let response = app.call(req).await.unwrap();
@@ -738,12 +781,13 @@ async fn overquota() {
738781
actix_rt::time::sleep(Duration::from_millis(10)).await;
739782

740783
let req = create_request(
741-
http::Method::PUT,
784+
&http::Method::PUT,
742785
"/1.5/42/storage/xxx_col2/12345",
743786
None,
744787
Some(json!(
745788
{"payload": "*".repeat(500)}
746789
)),
790+
Some(&settings),
747791
)
748792
.to_request();
749793
let response = app.call(req).await.unwrap();
@@ -765,7 +809,14 @@ async fn overquota() {
765809

766810
// XXX: this should run as cleanup regardless of test failure but it's
767811
// difficult. e.g. FutureExt::catch_unwind isn't compatible w/ actix-web
768-
let req = create_request(http::Method::DELETE, "/1.5/42/storage", None, None).to_request();
812+
let req = create_request(
813+
&http::Method::DELETE,
814+
"/1.5/42/storage",
815+
None,
816+
None,
817+
Some(&settings),
818+
)
819+
.to_request();
769820
let resp = app.call(req).await.unwrap();
770821
assert!(resp.response().status().is_success());
771822
}
@@ -778,7 +829,14 @@ async fn lbheartbeat_max_pool_size_check() {
778829
let app = init_app!(settings).await;
779830

780831
// Test all is well.
781-
let lb_req = create_request(http::Method::GET, "/__lbheartbeat__", None, None).to_request();
832+
let lb_req = create_request(
833+
&http::Method::GET,
834+
"/__lbheartbeat__",
835+
None,
836+
None,
837+
Some(&settings),
838+
)
839+
.to_request();
782840
let sresp = app.call(lb_req).await.unwrap();
783841
let status = sresp.status();
784842
// Uncomment only for debugging purposes:
@@ -790,10 +848,11 @@ async fn lbheartbeat_max_pool_size_check() {
790848
headers.insert("TEST_CONNECTIONS", "10".to_owned());
791849
headers.insert("TEST_IDLES", "0".to_owned());
792850
let req = create_request(
793-
http::Method::GET,
851+
&http::Method::GET,
794852
"/__lbheartbeat__",
795853
Some(headers.clone()),
796854
None,
855+
Some(&settings),
797856
)
798857
.to_request();
799858
let sresp = app.call(req).await.unwrap();
@@ -804,8 +863,14 @@ async fn lbheartbeat_max_pool_size_check() {
804863

805864
// check duration for exhausted connections
806865
actix_rt::time::sleep(Duration::from_secs(1)).await;
807-
let req =
808-
create_request(http::Method::GET, "/__lbheartbeat__", Some(headers), None).to_request();
866+
let req = create_request(
867+
&http::Method::GET,
868+
"/__lbheartbeat__",
869+
Some(headers),
870+
None,
871+
Some(&settings),
872+
)
873+
.to_request();
809874
let sresp = app.call(req).await.unwrap();
810875
let status = sresp.status();
811876
let body = test::read_body(sresp).await;
@@ -820,8 +885,14 @@ async fn lbheartbeat_max_pool_size_check() {
820885
let mut headers: HashMap<&str, String> = HashMap::new();
821886
headers.insert("TEST_CONNECTIONS", "5".to_owned());
822887
headers.insert("TEST_IDLES", "5".to_owned());
823-
let req =
824-
create_request(http::Method::GET, "/__lbheartbeat__", Some(headers), None).to_request();
888+
let req = create_request(
889+
&http::Method::GET,
890+
"/__lbheartbeat__",
891+
Some(headers),
892+
None,
893+
Some(&settings),
894+
)
895+
.to_request();
825896
let sresp = app.call(req).await.unwrap();
826897
let status = sresp.status();
827898
// Uncomment only for debugging purposes:
@@ -837,13 +908,27 @@ async fn lbheartbeat_ttl_check() {
837908

838909
let app = init_app!(settings).await;
839910

840-
let lb_req = create_request(http::Method::GET, "/__lbheartbeat__", None, None).to_request();
911+
let lb_req = create_request(
912+
&http::Method::GET,
913+
"/__lbheartbeat__",
914+
None,
915+
None,
916+
Some(&settings),
917+
)
918+
.to_request();
841919
let sresp = app.call(lb_req).await.unwrap();
842920
assert!(sresp.status().is_success());
843921

844922
actix_rt::time::sleep(Duration::from_secs(3)).await;
845923

846-
let lb_req = create_request(http::Method::GET, "/__lbheartbeat__", None, None).to_request();
924+
let lb_req = create_request(
925+
&http::Method::GET,
926+
"/__lbheartbeat__",
927+
None,
928+
None,
929+
Some(&settings),
930+
)
931+
.to_request();
847932
let sresp = app.call(lb_req).await.unwrap();
848933
assert_eq!(sresp.status(), StatusCode::INTERNAL_SERVER_ERROR);
849934
}

0 commit comments

Comments
 (0)