1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Net ;
5+ using System . Net . Http ;
6+ using System . Net . Http . Headers ;
7+ using System . Threading . Tasks ;
8+ using Microsoft . AspNetCore . Hosting ;
9+ using Microsoft . AspNetCore . TestHost ;
10+ using SqlStreamStore . Server . Browser ;
11+ using Xunit ;
12+
13+ namespace SqlStreamStore . Server . Tests . Browser
14+ {
15+ public class SqlStreamStoreBrowserTests : IDisposable
16+ {
17+ private readonly TestServer _server ;
18+ private readonly HttpClient _httpClient ;
19+
20+ public SqlStreamStoreBrowserTests ( )
21+ {
22+ _server = new TestServer (
23+ new WebHostBuilder ( )
24+ . Configure ( app => app . UseSqlStreamStoreBrowser ( typeof ( SqlStreamStoreBrowserTests ) ) ) ) ;
25+
26+ _httpClient = new HttpClient ( _server . CreateHandler ( ) )
27+ {
28+ BaseAddress = new UriBuilder ( ) . Uri
29+ } ;
30+ }
31+
32+ public static IEnumerable < object [ ] > IndexPageCases ( )
33+ {
34+ yield return new object [ ] { "/" } ;
35+ yield return new object [ ] { "/stream" } ;
36+ yield return new object [ ] { "/streams/a-stream" } ;
37+ yield return new object [ ] { "/streams/a-stream/metadata" } ;
38+ }
39+
40+ [ Theory , MemberData ( nameof ( IndexPageCases ) ) ]
41+ public async Task RequestsForHtmlReturnTheIndexPage ( string path )
42+ {
43+ using ( var response = await _httpClient . SendAsync ( new HttpRequestMessage ( HttpMethod . Get , path )
44+ {
45+ Headers = { Accept = { new MediaTypeWithQualityHeaderValue ( "text/html" ) } }
46+ } ) )
47+ {
48+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
49+ Assert . Equal ( await GetStaticEmbeddedResource ( "index.html" ) , await response . Content . ReadAsStringAsync ( ) ) ;
50+ }
51+ }
52+
53+ [ Fact ]
54+ public async Task RequestsForStaticFilesFromRootAreReturned ( )
55+ {
56+ using ( var response = await _httpClient . SendAsync (
57+ new HttpRequestMessage ( HttpMethod . Get , "/static/js/ws.js" )
58+ {
59+ Headers = { Accept = { new MediaTypeWithQualityHeaderValue ( "*/*" ) } }
60+ } ) )
61+ {
62+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
63+ Assert . Equal (
64+ await GetStaticEmbeddedResource ( "static.js.ws.js" ) ,
65+ await response . Content . ReadAsStringAsync ( ) ) ;
66+ }
67+ }
68+
69+ public static IEnumerable < object [ ] > StaticContentCases ( )
70+ {
71+ yield return new object [ ] { "/stream/" , "../../../" } ;
72+ yield return new object [ ] { "/streams/a-stream/" , "../../../../" } ;
73+ yield return new object [ ] { "/streams/a-stream/metadata/" , "../../../../../" } ;
74+ }
75+
76+ [ Theory , MemberData ( nameof ( StaticContentCases ) ) ]
77+ public async Task RequestsForStaticAreRedirectedIfNotAtRoot ( string path , string parent )
78+ {
79+ using ( var response = await _httpClient . SendAsync (
80+ new HttpRequestMessage ( HttpMethod . Get , $ "{ path } static/js/ws.js")
81+ {
82+ Headers = { Accept = { new MediaTypeWithQualityHeaderValue ( "*/*" ) } }
83+ } ) )
84+ {
85+ Assert . Equal ( HttpStatusCode . PermanentRedirect , response . StatusCode ) ;
86+ Assert . Equal ( $ "{ parent } static/js/ws.js", response . Headers . Location ? . ToString ( ) ) ;
87+ }
88+ }
89+
90+ private static async Task < string > GetStaticEmbeddedResource ( string resource )
91+ {
92+ using ( var stream = typeof ( SqlStreamStoreBrowserTests )
93+ . Assembly
94+ . GetManifestResourceStream ( typeof ( SqlStreamStoreBrowserTests ) , resource ) )
95+ using ( var reader = new StreamReader ( stream ) )
96+ {
97+ return await reader . ReadToEndAsync ( ) ;
98+ }
99+ }
100+
101+ public void Dispose ( )
102+ {
103+ _httpClient ? . Dispose ( ) ;
104+ _server ? . Dispose ( ) ;
105+ }
106+ }
107+ }
0 commit comments