@@ -6,80 +6,180 @@ macro_rules! b {
66
77mod mock {
88 use serde_json:: json;
9+ use std:: borrow:: Cow ;
910
1011 fn client < ' a > ( ) -> podcast_api:: Client < ' a > {
1112 podcast_api:: Client :: new ( None )
1213 }
1314
1415 #[ test]
1516 fn search ( ) {
16- let response = b ! ( client( ) . search( & json!( {
17- "q" : "dummy" ,
18- "sort_by_date" : 1
19- } ) ) )
20- . unwrap ( ) ;
21- assert ! ( response. is_object( ) ) ;
22- assert ! ( response[ "results" ] . as_array( ) . unwrap( ) . len( ) > 0 ) ;
17+ b ! ( async {
18+ let response = client( )
19+ . search( & json!( {
20+ "q" : "dummy" ,
21+ "sort_by_date" : 1
22+ } ) )
23+ . await
24+ . unwrap( ) ;
25+ // Request
26+ assert_eq!( response. request. method( ) , http:: Method :: GET ) ;
27+ assert_eq!( response. request. url( ) . path( ) , "/api/v2/search" ) ;
28+ let mut p = response. request. url( ) . query_pairs( ) ;
29+ assert_eq!( p. count( ) , 2 ) ;
30+ assert_eq!( p. next( ) , Some ( ( Cow :: Borrowed ( "q" ) , Cow :: Borrowed ( "dummy" ) ) ) ) ;
31+ assert_eq!(
32+ p. next( ) ,
33+ Some ( ( Cow :: Borrowed ( "sort_by_date" ) , Cow :: Borrowed ( "1" ) ) )
34+ ) ;
35+ // Response
36+ let body = response. json( ) . await . unwrap( ) ;
37+ assert!( body. is_object( ) ) ;
38+ assert!( body[ "results" ] . as_array( ) . unwrap( ) . len( ) > 0 ) ;
39+ } ) ;
2340 }
2441
2542 #[ test]
2643 fn typeahead ( ) {
27- let response = b ! ( client( ) . typeahead( & json!( {
28- "q" : "dummy" ,
29- "show_podcasts" : 1
30- } ) ) )
31- . unwrap ( ) ;
32- assert ! ( response. is_object( ) ) ;
33- assert ! ( response[ "terms" ] . as_array( ) . unwrap( ) . len( ) > 0 ) ;
44+ b ! ( async {
45+ let response = client( )
46+ . typeahead( & json!( {
47+ "q" : "dummy" ,
48+ "show_podcasts" : 1
49+ } ) )
50+ . await
51+ . unwrap( ) ;
52+ // Request
53+ assert_eq!( response. request. method( ) , http:: Method :: GET ) ;
54+ assert_eq!( response. request. url( ) . path( ) , "/api/v2/typeahead" ) ;
55+ let mut p = response. request. url( ) . query_pairs( ) ;
56+ assert_eq!( p. count( ) , 2 ) ;
57+ assert_eq!( p. next( ) , Some ( ( Cow :: Borrowed ( "q" ) , Cow :: Borrowed ( "dummy" ) ) ) ) ;
58+ assert_eq!(
59+ p. next( ) ,
60+ Some ( ( Cow :: Borrowed ( "show_podcasts" ) , Cow :: Borrowed ( "1" ) ) )
61+ ) ;
62+ // Response
63+ let body = response. json( ) . await . unwrap( ) ;
64+ assert!( body. is_object( ) ) ;
65+ assert!( body[ "terms" ] . as_array( ) . unwrap( ) . len( ) > 0 ) ;
66+ } ) ;
3467 }
3568
3669 #[ test]
3770 fn fetch_best_podcasts ( ) {
38- let response = b ! ( client( ) . fetch_best_podcasts( & json!( {
39- "genre_id" : 23 ,
40- } ) ) )
41- . unwrap ( ) ;
42- assert ! ( response. is_object( ) ) ;
43- assert ! ( response[ "total" ] . as_i64( ) . unwrap( ) > 0 ) ;
71+ b ! ( async {
72+ let response = client( )
73+ . fetch_best_podcasts( & json!( {
74+ "genre_id" : 23
75+ } ) )
76+ . await
77+ . unwrap( ) ;
78+ // Request
79+ assert_eq!( response. request. method( ) , http:: Method :: GET ) ;
80+ assert_eq!( response. request. url( ) . path( ) , "/api/v2/best_podcasts" ) ;
81+ let mut p = response. request. url( ) . query_pairs( ) ;
82+ assert_eq!( p. count( ) , 1 ) ;
83+ assert_eq!(
84+ p. next( ) ,
85+ Some ( ( Cow :: Borrowed ( "genre_id" ) , Cow :: Borrowed ( "23" ) ) )
86+ ) ;
87+ // Response
88+ let body = response. json( ) . await . unwrap( ) ;
89+ assert!( body. is_object( ) ) ;
90+ assert!( body[ "total" ] . as_i64( ) . unwrap( ) > 0 ) ;
91+ } ) ;
4492 }
4593
4694 #[ test]
4795 fn fetch_podcast_by_id ( ) {
48- let response = b ! ( client( ) . fetch_podcast_by_id( "dummy_id" , & json!( { } ) ) ) . unwrap ( ) ;
49- assert ! ( response. is_object( ) ) ;
50- assert ! ( response[ "episodes" ] . as_array( ) . unwrap( ) . len( ) > 0 ) ;
96+ b ! ( async {
97+ let response = client( )
98+ . fetch_podcast_by_id( "dummy_id" , & json!( { } ) )
99+ . await
100+ . unwrap( ) ;
101+ // Request
102+ assert_eq!( response. request. method( ) , http:: Method :: GET ) ;
103+ assert_eq!( response. request. url( ) . path( ) , "/api/v2/podcasts/dummy_id" ) ;
104+ let p = response. request. url( ) . query_pairs( ) ;
105+ assert_eq!( p. count( ) , 0 ) ;
106+ // Response
107+ let body = response. json( ) . await . unwrap( ) ;
108+ assert!( body. is_object( ) ) ;
109+ assert!( body[ "episodes" ] . as_array( ) . unwrap( ) . len( ) > 0 ) ;
110+ } ) ;
51111 }
52112
53113 #[ test]
54114 fn batch_fetch_podcasts ( ) {
55- let response = b ! ( client( ) . batch_fetch_podcasts( & json!( {
56- "ids" : "996,777,888,1000"
57- } ) ) )
58- . unwrap ( ) ;
59- assert ! ( response. is_object( ) ) ;
60- assert ! ( response[ "podcasts" ] . as_array( ) . unwrap( ) . len( ) > 0 ) ;
115+ b ! ( async {
116+ let response = client( )
117+ . batch_fetch_podcasts( & json!( {
118+ "ids" : "996,777,888,1000"
119+ } ) )
120+ . await
121+ . unwrap( ) ;
122+ // Request
123+ assert_eq!( response. request. method( ) , http:: Method :: POST ) ;
124+ assert_eq!( response. request. url( ) . path( ) , "/api/v2/podcasts" ) ;
125+ let mut p = form_urlencoded:: parse( response. request. body( ) . unwrap( ) . as_bytes( ) . unwrap( ) ) ;
126+ assert_eq!( p. count( ) , 1 ) ;
127+ assert_eq!(
128+ p. next( ) ,
129+ Some ( ( Cow :: Borrowed ( "ids" ) , Cow :: Borrowed ( "996,777,888,1000" ) ) )
130+ ) ;
131+ // Response
132+ let body = response. json( ) . await . unwrap( ) ;
133+ assert!( body. is_object( ) ) ;
134+ assert!( body[ "podcasts" ] . as_array( ) . unwrap( ) . len( ) > 0 ) ;
135+ } ) ;
61136 }
62137
63138 #[ test]
64139 fn fetch_episode_by_id ( ) {
65- let response = b ! ( client( ) . fetch_episode_by_id( "dummy_id" , & json!( { } ) ) ) . unwrap ( ) ;
66- assert ! ( response. is_object( ) ) ;
67- assert ! (
68- response[ "podcast" ] . as_object( ) . unwrap( ) [ "rss" ]
69- . as_str( )
70- . unwrap( )
71- . len( )
72- > 0
73- ) ;
140+ b ! ( async {
141+ let response = client( )
142+ . fetch_episode_by_id( "dummy_id" , & json!( { } ) )
143+ . await
144+ . unwrap( ) ;
145+ // Request
146+ assert_eq!( response. request. method( ) , http:: Method :: GET ) ;
147+ assert_eq!( response. request. url( ) . path( ) , "/api/v2/episodes/dummy_id" ) ;
148+ let p = response. request. url( ) . query_pairs( ) ;
149+ assert_eq!( p. count( ) , 0 ) ;
150+ // Response
151+ let body = response. json( ) . await . unwrap( ) ;
152+ assert!( body. is_object( ) ) ;
153+ assert!( body[ "podcast" ] . as_object( ) . unwrap( ) [ "rss" ]
154+ . as_str( )
155+ . unwrap( )
156+ . len( )
157+ > 0 ) ;
158+ } ) ;
74159 }
75160
76161 #[ test]
77162 fn batch_fetch_episodes ( ) {
78- let response = b ! ( client( ) . batch_fetch_episodes( & json!( {
79- "ids" : "996,777,888,1000"
80- } ) ) )
81- . unwrap ( ) ;
82- assert ! ( response. is_object( ) ) ;
83- assert ! ( response[ "episodes" ] . as_array( ) . unwrap( ) . len( ) > 0 ) ;
163+ b ! ( async {
164+ let response = client( )
165+ . batch_fetch_episodes( & json!( {
166+ "ids" : "996,777,888,1000"
167+ } ) )
168+ . await
169+ . unwrap( ) ;
170+ // Request
171+ assert_eq!( response. request. method( ) , http:: Method :: POST ) ;
172+ assert_eq!( response. request. url( ) . path( ) , "/api/v2/episodes" ) ;
173+ let mut p = form_urlencoded:: parse( response. request. body( ) . unwrap( ) . as_bytes( ) . unwrap( ) ) ;
174+ assert_eq!( p. count( ) , 1 ) ;
175+ assert_eq!(
176+ p. next( ) ,
177+ Some ( ( Cow :: Borrowed ( "ids" ) , Cow :: Borrowed ( "996,777,888,1000" ) ) )
178+ ) ;
179+ // Response
180+ let body = response. json( ) . await . unwrap( ) ;
181+ assert!( body. is_object( ) ) ;
182+ assert!( body[ "episodes" ] . as_array( ) . unwrap( ) . len( ) > 0 ) ;
183+ } ) ;
84184 }
85185}
0 commit comments