@@ -548,7 +548,7 @@ defmodule Mint.HTTP1Test do
548548 request_string ( """
549549 GET / HTTP/1.1
550550 host: localhost:#{ port }
551- user-agent: mint/ #{ Mix.Project . config ( ) [ :version ] }
551+ user-agent: #{ mint_user_agent ( ) }
552552
553553 \
554554 """ )
@@ -570,7 +570,7 @@ defmodule Mint.HTTP1Test do
570570 request_string ( """
571571 GET / HTTP/1.1
572572 host: localhost
573- user-agent: mint/ #{ Mix.Project . config ( ) [ :version ] }
573+ user-agent: #{ mint_user_agent ( ) }
574574
575575 \
576576 """ )
@@ -591,7 +591,7 @@ defmodule Mint.HTTP1Test do
591591 GET / HTTP/1.1
592592 content-length: 4
593593 host: localhost:#{ port }
594- user-agent: mint/ #{ Mix.Project . config ( ) [ :version ] }
594+ user-agent: #{ mint_user_agent ( ) }
595595
596596 body\
597597 """ )
@@ -607,7 +607,7 @@ defmodule Mint.HTTP1Test do
607607 request_string ( """
608608 GET / HTTP/1.1
609609 host: localhost:#{ port }
610- user-agent: mint/ #{ Mix.Project . config ( ) [ :version ] }
610+ user-agent: #{ mint_user_agent ( ) }
611611
612612 \
613613 """ )
@@ -626,7 +626,7 @@ defmodule Mint.HTTP1Test do
626626 request_string ( """
627627 GET / HTTP/1.1
628628 host: localhost:#{ port }
629- user-agent: mint/ #{ Mix.Project . config ( ) [ :version ] }
629+ user-agent: #{ mint_user_agent ( ) }
630630 content-length: 10
631631
632632 body\
@@ -760,6 +760,42 @@ defmodule Mint.HTTP1Test do
760760
761761 """ )
762762 end
763+
764+ @ invalid_request_targets [ "/ /" , "/%foo" , "/foo%x" ]
765+ test "targets are validated by default" , % { port: port , server_ref: server_ref } do
766+ assert { :ok , conn } = HTTP1 . connect ( :http , "localhost" , port )
767+
768+ assert_receive { ^ server_ref , _server_socket }
769+
770+ for invalid_target <- @ invalid_request_targets do
771+ assert { :error , % Mint.HTTP1 { } ,
772+ % Mint.HTTPError { reason: { :invalid_request_target , ^ invalid_target } } } =
773+ HTTP1 . request ( conn , "GET" , invalid_target , [ ] , "" )
774+ end
775+ end
776+
777+ test "target validation may be skipped based on connection options" , % {
778+ port: port ,
779+ server_ref: server_ref
780+ } do
781+ assert { :ok , conn } = HTTP1 . connect ( :http , "localhost" , port , skip_target_validation: true )
782+
783+ assert_receive { ^ server_ref , server_socket }
784+
785+ for invalid_target <- @ invalid_request_targets do
786+ assert { :ok , _conn , _ref } = HTTP1 . request ( conn , "GET" , invalid_target , [ ] , "body" )
787+
788+ assert receive_request_string ( server_socket ) ==
789+ request_string ( """
790+ GET #{ invalid_target } HTTP/1.1
791+ content-length: 4
792+ host: localhost:#{ port }
793+ user-agent: #{ mint_user_agent ( ) }
794+
795+ body\
796+ """ )
797+ end
798+ end
763799 end
764800
765801 describe "streaming requests" do
@@ -772,7 +808,7 @@ defmodule Mint.HTTP1Test do
772808 GET / HTTP/1.1
773809 transfer-encoding: chunked
774810 host: localhost:#{ port }
775- user-agent: mint/ #{ Mix.Project . config ( ) [ :version ] }
811+ user-agent: #{ mint_user_agent ( ) }
776812
777813 \
778814 """ )
@@ -795,7 +831,7 @@ defmodule Mint.HTTP1Test do
795831 request_string ( """
796832 GET / HTTP/1.1
797833 host: localhost:#{ port }
798- user-agent: mint/ #{ Mix.Project . config ( ) [ :version ] }
834+ user-agent: #{ mint_user_agent ( ) }
799835 transfer-encoding: chunked
800836
801837 \
@@ -815,7 +851,7 @@ defmodule Mint.HTTP1Test do
815851 request_string ( """
816852 GET / HTTP/1.1
817853 host: localhost:#{ port }
818- user-agent: mint/ #{ Mix.Project . config ( ) [ :version ] }
854+ user-agent: #{ mint_user_agent ( ) }
819855 transfer-encoding: gzip,chunked
820856
821857 \
@@ -839,7 +875,7 @@ defmodule Mint.HTTP1Test do
839875 request_string ( """
840876 GET / HTTP/1.1
841877 host: localhost:#{ port }
842- user-agent: mint/ #{ Mix.Project . config ( ) [ :version ] }
878+ user-agent: #{ mint_user_agent ( ) }
843879 transfer-encoding: identity
844880
845881 \
@@ -859,7 +895,7 @@ defmodule Mint.HTTP1Test do
859895 request_string ( """
860896 GET / HTTP/1.1
861897 host: localhost:#{ port }
862- user-agent: mint/ #{ Mix.Project . config ( ) [ :version ] }
898+ user-agent: #{ mint_user_agent ( ) }
863899 content-length: 5
864900
865901 \
@@ -877,7 +913,7 @@ defmodule Mint.HTTP1Test do
877913 GET / HTTP/1.1
878914 transfer-encoding: chunked
879915 host: localhost:#{ port }
880- user-agent: mint/ #{ Mix.Project . config ( ) [ :version ] }
916+ user-agent: #{ mint_user_agent ( ) }
881917
882918 \
883919 """ )
@@ -897,7 +933,7 @@ defmodule Mint.HTTP1Test do
897933 POST / HTTP/1.1
898934 transfer-encoding: chunked
899935 host: localhost:#{ port }
900- user-agent: mint/ #{ Mix.Project . config ( ) [ :version ] }
936+ user-agent: #{ mint_user_agent ( ) }
901937
902938 \
903939 """ )
@@ -997,4 +1033,7 @@ defmodule Mint.HTTP1Test do
9971033 defp stream_message_bytewise ( << >> , conn , responses ) do
9981034 { :ok , conn , responses }
9991035 end
1036+
1037+ @ mint_user_agent "mint/#{ Mix.Project . config ( ) [ :version ] } "
1038+ defp mint_user_agent , do: @ mint_user_agent
10001039end
0 commit comments