2121
2222#![ deny( unsafe_code) ]
2323
24- use percent_encoding:: { utf8_percent_encode, AsciiSet , CONTROLS } ;
2524use std:: fmt:: { Debug , Display , Formatter } ;
2625
26+ use percent_encoding:: { utf8_percent_encode, AsciiSet , CONTROLS } ;
27+
2728/// https://url.spec.whatwg.org/#fragment-percent-encode-set
2829const FRAGMENT : & AsciiSet = & CONTROLS . add ( b' ' ) . add ( b'"' ) . add ( b'<' ) . add ( b'>' ) . add ( b'`' ) ;
2930
@@ -218,7 +219,7 @@ impl QueryString {
218219impl Display for QueryString {
219220 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
220221 if self . pairs . is_empty ( ) {
221- return Ok ( ( ) ) ;
222+ Ok ( ( ) )
222223 } else {
223224 write ! ( f, "?" ) ?;
224225 for ( i, pair) in self . pairs . iter ( ) . enumerate ( ) {
@@ -247,6 +248,14 @@ struct Kvp {
247248mod tests {
248249 use super :: * ;
249250
251+ #[ test]
252+ fn test_empty ( ) {
253+ let qs = QueryString :: new ( ) ;
254+ assert_eq ! ( qs. to_string( ) , "" ) ;
255+ assert_eq ! ( qs. len( ) , 0 ) ;
256+ assert ! ( qs. is_empty( ) ) ;
257+ }
258+
250259 #[ test]
251260 fn test_simple ( ) {
252261 let qs = QueryString :: new ( )
@@ -256,6 +265,8 @@ mod tests {
256265 qs. to_string( ) ,
257266 "?q=apple???&category=fruits%20and%20vegetables"
258267 ) ;
268+ assert_eq ! ( qs. len( ) , 2 ) ;
269+ assert ! ( !qs. is_empty( ) ) ;
259270 }
260271
261272 #[ test]
@@ -287,5 +298,33 @@ mod tests {
287298 qs. to_string( ) ,
288299 "?q=celery&category=fruits%20and%20vegetables"
289300 ) ;
301+ assert_eq ! ( qs. len( ) , 2 ) ; // not three!
302+ }
303+
304+ #[ test]
305+ fn test_push_optional ( ) {
306+ let mut qs = QueryString :: new ( ) ;
307+ qs. push ( "a" , "apple" ) ;
308+ qs. push_opt ( "b" , None :: < String > ) ;
309+ qs. push_opt ( "c" , Some ( "🍎 apple" ) ) ;
310+
311+ assert_eq ! (
312+ format!( "https://example.com/{qs}" ) ,
313+ "https://example.com/?a=apple&c=%F0%9F%8D%8E%20apple"
314+ ) ;
315+ }
316+
317+ #[ test]
318+ fn test_append ( ) {
319+ let qs = QueryString :: new ( ) . with_value ( "q" , "apple" ) ;
320+ let more = QueryString :: new ( ) . with_value ( "q" , "pear" ) ;
321+
322+ let mut qs = qs. append_into ( more) ;
323+ qs. append ( QueryString :: new ( ) . with_value ( "answer" , "42" ) ) ;
324+
325+ assert_eq ! (
326+ format!( "https://example.com/{qs}" ) ,
327+ "https://example.com/?q=apple&q=pear&answer=42"
328+ ) ;
290329 }
291330}
0 commit comments