@@ -5,10 +5,18 @@ use std::{fs::canonicalize, process::exit, time::Duration};
5
5
use tokio:: time:: sleep;
6
6
7
7
/// creates a `cargo-shuttle` run instance with some reasonable defaults set.
8
- async fn cargo_shuttle_run ( working_directory : & str ) -> u16 {
8
+ async fn cargo_shuttle_run ( working_directory : & str , external : bool ) -> String {
9
9
let working_directory = canonicalize ( working_directory) . unwrap ( ) ;
10
+
10
11
let port = pick_unused_port ( ) . unwrap ( ) ;
11
- let run_args = RunArgs { port } ;
12
+
13
+ let url = if !external {
14
+ format ! ( "http://localhost:{port}" )
15
+ } else {
16
+ format ! ( "http://0.0.0.0:{port}" )
17
+ } ;
18
+
19
+ let run_args = RunArgs { port, external } ;
12
20
13
21
let runner = Shuttle :: new ( ) . unwrap ( ) . run ( Args {
14
22
api_url : Some ( "http://shuttle.invalid:80" . to_string ( ) ) ,
@@ -34,28 +42,23 @@ async fn cargo_shuttle_run(working_directory: &str) -> u16 {
34
42
tokio:: spawn ( runner) ;
35
43
36
44
// Wait for service to be responsive
37
- while ( reqwest:: Client :: new ( )
38
- . get ( format ! ( "http://localhost:{port}" ) )
39
- . send ( )
40
- . await )
41
- . is_err ( )
42
- {
45
+ while ( reqwest:: Client :: new ( ) . get ( url. clone ( ) ) . send ( ) . await ) . is_err ( ) {
43
46
println ! (
44
47
"waiting for '{}' to start up..." ,
45
48
working_directory. display( )
46
49
) ;
47
50
sleep ( Duration :: from_millis ( 350 ) ) . await ;
48
51
}
49
52
50
- port
53
+ url
51
54
}
52
55
53
56
#[ tokio:: test( flavor = "multi_thread" ) ]
54
57
async fn rocket_hello_world ( ) {
55
- let port = cargo_shuttle_run ( "../examples/rocket/hello-world" ) . await ;
58
+ let url = cargo_shuttle_run ( "../examples/rocket/hello-world" , false ) . await ;
56
59
57
60
let request_text = reqwest:: Client :: new ( )
58
- . get ( format ! ( "http://localhost:{port }/hello" ) )
61
+ . get ( format ! ( "{url }/hello" ) )
59
62
. send ( )
60
63
. await
61
64
. unwrap ( )
@@ -68,10 +71,10 @@ async fn rocket_hello_world() {
68
71
69
72
#[ tokio:: test( flavor = "multi_thread" ) ]
70
73
async fn rocket_secrets ( ) {
71
- let port = cargo_shuttle_run ( "../examples/rocket/secrets" ) . await ;
74
+ let url = cargo_shuttle_run ( "../examples/rocket/secrets" , false ) . await ;
72
75
73
76
let request_text = reqwest:: Client :: new ( )
74
- . get ( format ! ( "http://localhost:{port }/secret" ) )
77
+ . get ( format ! ( "{url }/secret" ) )
75
78
. send ( )
76
79
. await
77
80
. unwrap ( )
@@ -85,11 +88,11 @@ async fn rocket_secrets() {
85
88
// This example uses a shared Postgres. Thus local runs should create a docker container for it.
86
89
#[ tokio:: test( flavor = "multi_thread" ) ]
87
90
async fn rocket_postgres ( ) {
88
- let port = cargo_shuttle_run ( "../examples/rocket/postgres" ) . await ;
91
+ let url = cargo_shuttle_run ( "../examples/rocket/postgres" , false ) . await ;
89
92
let client = reqwest:: Client :: new ( ) ;
90
93
91
94
let post_text = client
92
- . post ( format ! ( "http://localhost:{port }/todo" ) )
95
+ . post ( format ! ( "{url }/todo" ) )
93
96
. body ( "{\" note\" : \" Deploy to shuttle\" }" )
94
97
. send ( )
95
98
. await
@@ -101,7 +104,7 @@ async fn rocket_postgres() {
101
104
assert_eq ! ( post_text, "{\" id\" :1,\" note\" :\" Deploy to shuttle\" }" ) ;
102
105
103
106
let request_text = client
104
- . get ( format ! ( "http://localhost:{port }/todo/1" ) )
107
+ . get ( format ! ( "{url }/todo/1" ) )
105
108
. send ( )
106
109
. await
107
110
. unwrap ( )
@@ -114,11 +117,11 @@ async fn rocket_postgres() {
114
117
115
118
#[ tokio:: test( flavor = "multi_thread" ) ]
116
119
async fn rocket_authentication ( ) {
117
- let port = cargo_shuttle_run ( "../examples/rocket/authentication" ) . await ;
120
+ let url = cargo_shuttle_run ( "../examples/rocket/authentication" , false ) . await ;
118
121
let client = reqwest:: Client :: new ( ) ;
119
122
120
123
let public_text = client
121
- . get ( format ! ( "http://localhost:{port }/public" ) )
124
+ . get ( format ! ( "{url }/public" ) )
122
125
. send ( )
123
126
. await
124
127
. unwrap ( )
@@ -132,7 +135,7 @@ async fn rocket_authentication() {
132
135
) ;
133
136
134
137
let private_status = client
135
- . get ( format ! ( "http://localhost:{port }/private" ) )
138
+ . get ( format ! ( "{url }/private" ) )
136
139
. send ( )
137
140
. await
138
141
. unwrap ( )
@@ -141,7 +144,7 @@ async fn rocket_authentication() {
141
144
assert_eq ! ( private_status, StatusCode :: FORBIDDEN ) ;
142
145
143
146
let body = client
144
- . post ( format ! ( "http://localhost:{port }/login" ) )
147
+ . post ( format ! ( "{url }/login" ) )
145
148
. body ( "{\" username\" : \" username\" , \" password\" : \" password\" }" )
146
149
. send ( )
147
150
. await
@@ -153,7 +156,7 @@ async fn rocket_authentication() {
153
156
let token = format ! ( "Bearer {}" , json[ "token" ] . as_str( ) . unwrap( ) ) ;
154
157
155
158
let private_text = client
156
- . get ( format ! ( "http://localhost:{port }/private" ) )
159
+ . get ( format ! ( "{url }/private" ) )
157
160
. header ( "Authorization" , token)
158
161
. send ( )
159
162
. await
@@ -170,10 +173,10 @@ async fn rocket_authentication() {
170
173
171
174
#[ tokio:: test( flavor = "multi_thread" ) ]
172
175
async fn actix_web_hello_world ( ) {
173
- let port = cargo_shuttle_run ( "../examples/actix-web/hello-world" ) . await ;
176
+ let url = cargo_shuttle_run ( "../examples/actix-web/hello-world" , false ) . await ;
174
177
175
178
let request_text = reqwest:: Client :: new ( )
176
- . get ( format ! ( "http://localhost:{port }/hello" ) )
179
+ . get ( format ! ( "{url }/hello" ) )
177
180
. send ( )
178
181
. await
179
182
. unwrap ( )
@@ -186,10 +189,10 @@ async fn actix_web_hello_world() {
186
189
187
190
#[ tokio:: test( flavor = "multi_thread" ) ]
188
191
async fn axum_hello_world ( ) {
189
- let port = cargo_shuttle_run ( "../examples/axum/hello-world" ) . await ;
192
+ let url = cargo_shuttle_run ( "../examples/axum/hello-world" , false ) . await ;
190
193
191
194
let request_text = reqwest:: Client :: new ( )
192
- . get ( format ! ( "http://localhost:{port }/hello" ) )
195
+ . get ( format ! ( "{url }/hello" ) )
193
196
. send ( )
194
197
. await
195
198
. unwrap ( )
@@ -202,10 +205,10 @@ async fn axum_hello_world() {
202
205
203
206
#[ tokio:: test( flavor = "multi_thread" ) ]
204
207
async fn tide_hello_world ( ) {
205
- let port = cargo_shuttle_run ( "../examples/tide/hello-world" ) . await ;
208
+ let url = cargo_shuttle_run ( "../examples/tide/hello-world" , false ) . await ;
206
209
207
210
let request_text = reqwest:: Client :: new ( )
208
- . get ( format ! ( "http://localhost:{port }/hello" ) )
211
+ . get ( format ! ( "{url }/hello" ) )
209
212
. send ( )
210
213
. await
211
214
. unwrap ( )
@@ -218,10 +221,10 @@ async fn tide_hello_world() {
218
221
219
222
#[ tokio:: test( flavor = "multi_thread" ) ]
220
223
async fn tower_hello_world ( ) {
221
- let port = cargo_shuttle_run ( "../examples/tower/hello-world" ) . await ;
224
+ let url = cargo_shuttle_run ( "../examples/tower/hello-world" , false ) . await ;
222
225
223
226
let request_text = reqwest:: Client :: new ( )
224
- . get ( format ! ( "http://localhost:{port }/hello" ) )
227
+ . get ( format ! ( "{url }/hello" ) )
225
228
. send ( )
226
229
. await
227
230
. unwrap ( )
@@ -234,10 +237,10 @@ async fn tower_hello_world() {
234
237
235
238
#[ tokio:: test( flavor = "multi_thread" ) ]
236
239
async fn warp_hello_world ( ) {
237
- let port = cargo_shuttle_run ( "../examples/warp/hello-world" ) . await ;
240
+ let url = cargo_shuttle_run ( "../examples/warp/hello-world" , false ) . await ;
238
241
239
242
let request_text = reqwest:: Client :: new ( )
240
- . get ( format ! ( "http://localhost:{port }/hello" ) )
243
+ . get ( format ! ( "{url }/hello" ) )
241
244
. send ( )
242
245
. await
243
246
. unwrap ( )
@@ -250,10 +253,10 @@ async fn warp_hello_world() {
250
253
251
254
#[ tokio:: test( flavor = "multi_thread" ) ]
252
255
async fn poem_hello_world ( ) {
253
- let port = cargo_shuttle_run ( "../examples/poem/hello-world" ) . await ;
256
+ let url = cargo_shuttle_run ( "../examples/poem/hello-world" , false ) . await ;
254
257
255
258
let request_text = reqwest:: Client :: new ( )
256
- . get ( format ! ( "http://localhost:{port }/hello" ) )
259
+ . get ( format ! ( "{url }/hello" ) )
257
260
. send ( )
258
261
. await
259
262
. unwrap ( )
@@ -267,11 +270,11 @@ async fn poem_hello_world() {
267
270
// This example uses a shared Postgres. Thus local runs should create a docker container for it.
268
271
#[ tokio:: test( flavor = "multi_thread" ) ]
269
272
async fn poem_postgres ( ) {
270
- let port = cargo_shuttle_run ( "../examples/poem/postgres" ) . await ;
273
+ let url = cargo_shuttle_run ( "../examples/poem/postgres" , false ) . await ;
271
274
let client = reqwest:: Client :: new ( ) ;
272
275
273
276
let post_text = client
274
- . post ( format ! ( "http://localhost:{port }/todo" ) )
277
+ . post ( format ! ( "{url }/todo" ) )
275
278
. body ( "{\" note\" : \" Deploy to shuttle\" }" )
276
279
. header ( "content-type" , "application/json" )
277
280
. send ( )
@@ -284,7 +287,7 @@ async fn poem_postgres() {
284
287
assert_eq ! ( post_text, "{\" id\" :1,\" note\" :\" Deploy to shuttle\" }" ) ;
285
288
286
289
let request_text = client
287
- . get ( format ! ( "http://localhost:{port }/todo/1" ) )
290
+ . get ( format ! ( "{url }/todo/1" ) )
288
291
. send ( )
289
292
. await
290
293
. unwrap ( )
@@ -298,12 +301,12 @@ async fn poem_postgres() {
298
301
// This example uses a shared MongoDb. Thus local runs should create a docker container for it.
299
302
#[ tokio:: test( flavor = "multi_thread" ) ]
300
303
async fn poem_mongodb ( ) {
301
- let port = cargo_shuttle_run ( "../examples/poem/mongodb" ) . await ;
304
+ let url = cargo_shuttle_run ( "../examples/poem/mongodb" , false ) . await ;
302
305
let client = reqwest:: Client :: new ( ) ;
303
306
304
307
// Post a todo note and get the persisted todo objectId
305
308
let post_text = client
306
- . post ( format ! ( "http://localhost:{port }/todo" ) )
309
+ . post ( format ! ( "{url }/todo" ) )
307
310
. body ( "{\" note\" : \" Deploy to shuttle\" }" )
308
311
. header ( "content-type" , "application/json" )
309
312
. send ( )
@@ -317,7 +320,7 @@ async fn poem_mongodb() {
317
320
assert_eq ! ( post_text. len( ) , 24 ) ;
318
321
319
322
let request_text = client
320
- . get ( format ! ( "http://localhost:{port }/todo/{post_text}" ) )
323
+ . get ( format ! ( "{url }/todo/{post_text}" ) )
321
324
. send ( )
322
325
. await
323
326
. unwrap ( )
@@ -330,10 +333,10 @@ async fn poem_mongodb() {
330
333
331
334
#[ tokio:: test( flavor = "multi_thread" ) ]
332
335
async fn salvo_hello_world ( ) {
333
- let port = cargo_shuttle_run ( "../examples/salvo/hello-world" ) . await ;
336
+ let url = cargo_shuttle_run ( "../examples/salvo/hello-world" , false ) . await ;
334
337
335
338
let request_text = reqwest:: Client :: new ( )
336
- . get ( format ! ( "http://localhost:{port }/hello" ) )
339
+ . get ( format ! ( "{url }/hello" ) )
337
340
. send ( )
338
341
. await
339
342
. unwrap ( )
@@ -346,10 +349,10 @@ async fn salvo_hello_world() {
346
349
347
350
#[ tokio:: test( flavor = "multi_thread" ) ]
348
351
async fn thruster_hello_world ( ) {
349
- let port = cargo_shuttle_run ( "../examples/thruster/hello-world" ) . await ;
352
+ let url = cargo_shuttle_run ( "../examples/thruster/hello-world" , false ) . await ;
350
353
351
354
let request_text = reqwest:: Client :: new ( )
352
- . get ( format ! ( "http://localhost:{port }/hello" ) )
355
+ . get ( format ! ( "{url }/hello" ) )
353
356
. send ( )
354
357
. await
355
358
. unwrap ( )
@@ -359,3 +362,19 @@ async fn thruster_hello_world() {
359
362
360
363
assert_eq ! ( request_text, "Hello, World!" ) ;
361
364
}
365
+
366
+ #[ tokio:: test( flavor = "multi_thread" ) ]
367
+ async fn rocket_hello_world_with_router_ip ( ) {
368
+ let url = cargo_shuttle_run ( "../examples/rocket/hello-world" , true ) . await ;
369
+
370
+ let request_text = reqwest:: Client :: new ( )
371
+ . get ( format ! ( "{url}/hello" ) )
372
+ . send ( )
373
+ . await
374
+ . unwrap ( )
375
+ . text ( )
376
+ . await
377
+ . unwrap ( ) ;
378
+
379
+ assert_eq ! ( request_text, "Hello, world!" ) ;
380
+ }
0 commit comments