From 8987ad26b25be20988a2cc3a71ad1b38d2a2845b Mon Sep 17 00:00:00 2001 From: Luke Thompson Date: Fri, 26 May 2023 15:38:45 +1000 Subject: [PATCH 1/5] implement a get service that returns some structure --- src/main.rs | 11 ++++++++--- src/tests.rs | 9 +++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index fb370fe..833bd3d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,9 +4,14 @@ #[cfg(test)] mod tests; -#[get("/")] -fn hello() -> &'static str { - "Hello, world!" +#[get("/barcode/")] +fn hello(barcode: String) -> String { + format!("{{\"barcode\": \"{barcode}\", \ + \"name\": \"Test Product\", \ + \"price\": 100, \ + \"image\": \"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png\"\ + }}\ + ") } fn main() { diff --git a/src/tests.rs b/src/tests.rs index da0b732..502cbd9 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -5,6 +5,11 @@ use rocket::local::Client; fn hello_world() { let rocket = rocket::ignite().mount("/", routes![super::hello]); let client = Client::new(rocket).unwrap(); - let mut response = client.get("/").dispatch(); - assert_eq!(response.body_string(), Some("Hello, world!".into())); + let mut response = client.get("/barcode/1234").dispatch(); + assert_eq!(response.body_string(), Some("{\"barcode\": \"1234\", \ + \"name\": \"Test Product\", \ + \"price\": 100, \ + \"image\": \"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png\"\ + }\ +".into())); } From f1beedf9b1f452193c48e68dfabb993e52e5ec51 Mon Sep 17 00:00:00 2001 From: Luke Thompson Date: Fri, 26 May 2023 16:01:16 +1000 Subject: [PATCH 2/5] add real properties for the barcode --- src/main.rs | 12 +++++++----- src/tests.rs | 6 ++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 833bd3d..8b2d193 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,11 +7,13 @@ #[get("/barcode/")] fn hello(barcode: String) -> String { format!("{{\"barcode\": \"{barcode}\", \ - \"name\": \"Test Product\", \ - \"price\": 100, \ - \"image\": \"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png\"\ - }}\ - ") + \"name\": \"Grape Test\", \ + \"fruit\": \"grapes\", \ + \"process\": \"main\", \ + \"standard\": \"premium\", \ + \"image\": \"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png\"\ + }}\ +") } fn main() { diff --git a/src/tests.rs b/src/tests.rs index 502cbd9..fe0d572 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -7,8 +7,10 @@ fn hello_world() { let client = Client::new(rocket).unwrap(); let mut response = client.get("/barcode/1234").dispatch(); assert_eq!(response.body_string(), Some("{\"barcode\": \"1234\", \ - \"name\": \"Test Product\", \ - \"price\": 100, \ + \"name\": \"Grape Test\", \ + \"fruit\": \"grapes\", \ + \"process\": \"main\", \ + \"standard\": \"premium\", \ \"image\": \"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png\"\ }\ ".into())); From dc72353ab111a0a2e0ec675ac18618cf33a37eb1 Mon Sep 17 00:00:00 2001 From: Luke Thompson Date: Fri, 26 May 2023 16:11:25 +1000 Subject: [PATCH 3/5] add the market proeprty --- src/main.rs | 20 ++++++++++++++++++-- src/tests.rs | 5 +++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8b2d193..fbd1ea4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,18 +4,34 @@ #[cfg(test)] mod tests; +#[get("/")] +fn home() -> String { + format!(" + + Barcode API + + +

Barcode API

+

Use the following URL to get a barcode:

+

https://barcode-api.herokuapp.com/barcode/1234

+ + +") +} + #[get("/barcode/")] -fn hello(barcode: String) -> String { +fn barcode(barcode: String) -> String { format!("{{\"barcode\": \"{barcode}\", \ \"name\": \"Grape Test\", \ \"fruit\": \"grapes\", \ \"process\": \"main\", \ \"standard\": \"premium\", \ + \"market\": \"Harris Farms\", \ \"image\": \"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png\"\ }}\ ") } fn main() { - rocket::ignite().mount("/", routes![hello]).launch(); + rocket::ignite().mount("/", routes![barcode, home]).launch(); } diff --git a/src/tests.rs b/src/tests.rs index fe0d572..17d40de 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -2,8 +2,8 @@ use super::rocket; use rocket::local::Client; #[test] -fn hello_world() { - let rocket = rocket::ignite().mount("/", routes![super::hello]); +fn barcode() { + let rocket = rocket::ignite().mount("/", routes![super::barcode]); let client = Client::new(rocket).unwrap(); let mut response = client.get("/barcode/1234").dispatch(); assert_eq!(response.body_string(), Some("{\"barcode\": \"1234\", \ @@ -11,6 +11,7 @@ fn hello_world() { \"fruit\": \"grapes\", \ \"process\": \"main\", \ \"standard\": \"premium\", \ + \"market\": \"Harris Farms\", \ \"image\": \"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png\"\ }\ ".into())); From 17a4bc9b66de435f64ac82fbc00fbc1cb787cf2e Mon Sep 17 00:00:00 2001 From: Luke Thompson Date: Fri, 26 May 2023 16:14:52 +1000 Subject: [PATCH 4/5] Test for woolworths market PR environment --- src/main.rs | 2 +- src/tests.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index fbd1ea4..bd3262c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,7 @@ fn barcode(barcode: String) -> String { \"fruit\": \"grapes\", \ \"process\": \"main\", \ \"standard\": \"premium\", \ - \"market\": \"Harris Farms\", \ + \"market\": \"Woolworths\", \ \"image\": \"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png\"\ }}\ ") diff --git a/src/tests.rs b/src/tests.rs index 17d40de..6992735 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -11,7 +11,7 @@ fn barcode() { \"fruit\": \"grapes\", \ \"process\": \"main\", \ \"standard\": \"premium\", \ - \"market\": \"Harris Farms\", \ + \"market\": \"Woolworths\", \ \"image\": \"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png\"\ }\ ".into())); From f25807ce22dff7a5441d25c990597c6df9debfe5 Mon Sep 17 00:00:00 2001 From: Luke Thompson Date: Fri, 26 May 2023 16:31:34 +1000 Subject: [PATCH 5/5] return HTML from the getter --- src/main.rs | 7 +++++-- src/tests.rs | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index bd3262c..b549616 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,14 @@ #![feature(proc_macro_hygiene, decl_macro)] +use rocket::response::content::Html; + #[macro_use] extern crate rocket; #[cfg(test)] mod tests; #[get("/")] -fn home() -> String { +fn home() -> Html { + Html( format!(" Barcode API @@ -16,7 +19,7 @@ fn home() -> String {

https://barcode-api.herokuapp.com/barcode/1234

-") +")) } #[get("/barcode/")] diff --git a/src/tests.rs b/src/tests.rs index 6992735..075d276 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -16,3 +16,21 @@ fn barcode() { }\ ".into())); } + +#[test] +fn home() { + let rocket = rocket::ignite().mount("/", routes![super::home]); + let client = Client::new(rocket).unwrap(); + let mut response = client.get("/").dispatch(); + assert_eq!(response.body_string(), Some(" + + Barcode API + + +

Barcode API

+

Use the following URL to get a barcode:

+

https://barcode-api.herokuapp.com/barcode/1234

+ + +".into())) +}