33
44use Authwave \AuthUri ;
55use Authwave \InitVector ;
6+ use Authwave \InsecureProtocolException ;
67use Authwave \Token ;
78use PHPUnit \Framework \TestCase ;
89use Psr \Http \Message \UriInterface ;
@@ -14,7 +15,49 @@ public function testAuthUriHttps() {
1415 ->willReturn ("https://example.com " );
1516 $ token = self ::createMock (Token::class);
1617
17- $ sut = new AuthUri ($ baseUri , $ token , "" );
18+ $ sut = new AuthUri ($ token , $ baseUri , "" );
19+ self ::assertEquals (
20+ "https " ,
21+ $ sut ->getScheme ()
22+ );
23+ }
24+
25+ // All AuthUris MUST be served over HTTPS, with the one exception of localhost.
26+ // But it should still default to HTTPS on localhost.
27+ public function testGetAuthUriHostnameLocalhostHttpsByDefault () {
28+ $ token = self ::createMock (Token::class);
29+ $ sut = new AuthUri ($ token , "localhost " );
30+ self ::assertStringStartsWith (
31+ "https://localhost " ,
32+ $ sut
33+ );
34+ }
35+
36+ // We should be able to set the scheme to HTTP for localhost hostname only.
37+ public function testGetAuthUriHostnameLocalhostHttpAllowed () {
38+ $ token = self ::createMock (Token::class);
39+ $ sut = new AuthUri ($ token , "http://localhost " );
40+ self ::assertStringStartsWith (
41+ "http://localhost " ,
42+ $ sut
43+ );
44+ }
45+
46+ // We should NOT be able to set the scheme to HTTP for other hostnames.
47+ public function testGetAuthUriHostnameNotLocalhostHttpNotAllowed () {
48+ $ token = self ::createMock (Token::class);
49+ self ::expectException (InsecureProtocolException::class);
50+ new AuthUri ($ token , "http://localhost.com " );
51+ }
52+
53+ public function testAuthUriHttpsInferred () {
54+ $ baseUri = self ::createMock (UriInterface::class);
55+ $ baseUri ->method ("__toString " )
56+ ->willReturn ("example.com " );
57+ // Note on the line above, no scheme is passed in - we must assume https.
58+ $ token = self ::createMock (Token::class);
59+
60+ $ sut = new AuthUri ($ token , $ baseUri , "" );
1861 self ::assertEquals (
1962 "https " ,
2063 $ sut ->getScheme ()
@@ -36,7 +79,7 @@ public function testQueryString() {
3679 ->willReturn ($ iv );
3780
3881 $ returnPath = "/examplePage " ;
39- $ sut = new AuthUri ($ baseUri , $ token , $ returnPath );
82+ $ sut = new AuthUri ($ token , $ baseUri , $ returnPath );
4083 parse_str ($ sut ->getQuery (), $ queryParts );
4184
4285 self ::assertEquals (
0 commit comments