/* Ċ Ġ* ĠCopyright Ġ 2 0 1 5 - 2 0 1 9 ĠThe ĠOpen Zip kin ĠAuthors Ċ Ġ* Ċ Ġ* ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ċ Ġ* Ġin Ġcompliance Ġwith Ġthe ĠLicense . ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ Ġ* Ċ Ġ* Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ Ġ* Ċ Ġ* ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ġdistributed Ġunder Ġthe ĠLicense Ċ Ġ* Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ċ Ġ* Ġor Ġimplied . ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ġlimitations Ġunder Ċ Ġ* Ġthe ĠLicense . Ċ Ġ*/ Ċ package Ġzip kin 2 . server . internal . ui Ċ Ċ import Ġcom . line corp . ar mer ia . client . HttpClient Ċ import Ġcom . line corp . ar mer ia . common . Http Header Names Ċ import Ġcom . line corp . ar mer ia . common . HttpMethod Ċ import Ġcom . line corp . ar mer ia . common . Request Headers Ċ import Ġcom . line corp . ar mer ia . server . Server Ċ import Ġokhttp 3 . Headers Ċ import Ġok io . Ok io Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . boot . autoconfigure . Enable Auto Configuration Ċ import Ġorg . springframework . boot . test . context . SpringBootTest Ċ import Ġorg . springframework . context . annotation . Import Ċ import Ġorg . springframework . test . context . junit 4 . Spring Runner Ċ import Ġzip kin 2 . server . internal . Http Ċ Ċ @ RunWith ( Spring Runner :: class ) Ċ @ SpringBootTest ( ĊĠ Ġclasses Ġ= Ġ[ IT Zip kin Ui Configuration . Test Server :: class ], ĊĠ Ġweb Environment Ġ= ĠSpring Boot Test . Web Environment . RANDOM _ PORT , ĊĠ Ġproperties Ġ= Ġ[ ĊĠĠĠ Ġ" zip kin . ui . base - path =/ foo zip kin ", ĊĠĠĠ Ġ" server . compression . enabled = true ", ĊĠĠĠ Ġ" server . compression . min - response - size = 1 2 8 " ĊĠ Ġ] Ċ ) Ċ class ĠIT Zip kin Ui Configuration Ġ{ ĊĠ Ġ@ Autowired Ġlateinit Ġvar Ġserver : ĠServer ĊĊĠ Ġ/** ĠThe Ġzip kin - ui Ġis Ġa Ġsingle - page Ġapp . ĠThis Ġprevents Ġre loading Ġall Ġresources Ġon Ġeach Ġclick . Ġ Ġ*/ ĊĠ Ġ@ Test Ġfun Ġsets Max Age On Ui Resources () Ġ{ ĊĠĠĠ ĠassertThat ( Http . get ( server , Ġ"/ zip kin / config . json "). header (" Cache - Control ")) ĊĠĠĠĠĠ Ġ. isEqualTo (" max - age = 6 0 0 ") ĊĠĠĠ ĠassertThat ( Http . get ( server , Ġ"/ zip kin / index . html "). header (" Cache - Control ")) ĊĠĠĠĠĠ Ġ. isEqualTo (" max - age = 6 0 ") ĊĠĠĠ ĠassertThat ( Http . get ( server , Ġ"/ zip kin / test . txt "). header (" Cache - Control ")) ĊĠĠĠĠĠ Ġ. isEqualTo (" max - age = 3 1 5 3 6 0 0 0 ") ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġredirects Index () Ġ{ ĊĠĠĠ Ġval Ġindex Ġ= ĠHttp . get AsString ( server , Ġ"/ zip kin / index . html ") ĊĠĠĠ ĠassertThat ( Http . get AsString ( server , Ġ"/ zip kin / ")). isEqualTo ( index ) ĊĊĠĠĠ ĠlistOf ("/ zip kin ", Ġ"/ "). forEach Ġ{ Ġpath Ġ-> ĊĠĠĠĠĠ Ġval Ġresponse Ġ= ĠHttp . get ( server , Ġpath ) ĊĠĠĠĠĠ ĠassertThat ( response . code ()). isEqualTo ( 3 0 2 ) ĊĠĠĠĠĠ ĠassertThat ( response . header (" location ")). isEqualTo ("/ zip kin /") ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ/** ĠB rowsers Ġhon or Ġconditional Ġrequests Ġsuch Ġas Ġe Tag . ĠLet 's Ġmake Ġsure Ġthe Ġserver Ġdoes Ġ Ġ*/ ĊĠ Ġ@ Test Ġfun Ġconditional Requests () Ġ{ ĊĠĠĠ ĠlistOf ("/ zip kin / config . json ", Ġ"/ zip kin / index . html ", Ġ"/ zip kin / test . txt "). forEach Ġ{ Ġpath Ġ-> ĊĠĠĠĠĠ Ġval Ġet ag Ġ= ĠHttp . get ( server , Ġpath ). header (" etag ") ĊĠĠĠĠĠ ĠassertThat ( Http . get ( server , Ġpath , ĠHeaders . of (" If - None - Match ", Ġet ag )). code ()) ĊĠĠĠĠĠĠĠ Ġ. isEqualTo ( 3 0 4 ) ĊĠĠĠĠĠ ĠassertThat ( Http . get ( server , Ġpath , ĠHeaders . of (" If - None - Match ", Ġ" a arg h ")). code ()) ĊĠĠĠĠĠĠĠ Ġ. isEqualTo ( 2 0 0 ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ/** ĠSome Ġassets Ġare Ġpretty Ġbig . Ġensure Ġthey Ġuse Ġcompression . Ġ Ġ*/ ĊĠ Ġ@ Test Ġfun Ġsupports Compression () Ġ{ ĊĠĠĠ ĠassertThat ( getContent Encoding From Request That Accept s G zip ("/ zip kin / test . txt ")) ĊĠĠĠĠĠ Ġ. isNull () Ġ// Ġtoo Ġsmall Ġto Ġcompress ĊĠĠĠ ĠassertThat ( getContent Encoding From Request That Accept s G zip ("/ zip kin / config . json ")) ĊĠĠĠĠĠ Ġ. isEqualTo (" gzip ") ĊĠ Ġ} ĊĊĠ Ġ/** ĊĠĠ Ġ* ĠThe Ġtest Ġsets Ġthe Ġproperty Ġ` zip kin . ui . base - path =/ foo zip kin `, Ġwhich Ġshould Ġreflect Ġin ĊĠĠ Ġ* Ġindex . html ĊĠĠ Ġ*/ ĊĠ Ġ@ Test Ġfun Ġreplaces Base Tag () Ġ{ ĊĠĠĠ ĠassertThat ( Http . get AsString ( server , Ġ"/ zip kin / index . html ")) ĊĠĠĠĠĠ Ġ. isEqualTo Ign oring Whitespace ( string From Classpath (" zip kin - ui / index . html ") ĊĠĠĠĠĠĠĠ Ġ. replace ("< base Ġhref =\" / \" Ġ/> ", Ġ"< base Ġhref =\" / foo zip kin / \"> ")) ĊĠ Ġ} ĊĊĠ Ġ/** Ġindex . html Ġis Ġserved Ġseparately . ĠThis Ġtests Ġother Ġcontent Ġis Ġalso Ġloaded Ġfrom Ġthe Ġclasspath . Ġ Ġ*/ ĊĠ Ġ@ Test Ġfun Ġserves Other Content From Classpath () Ġ{ ĊĠĠĠ ĠassertThat ( Http . get AsString ( server , Ġ"/ zip kin / test . txt ")) ĊĠĠĠĠĠ Ġ. isEqualTo Ign oring Whitespace ( string From Classpath (" zip kin - ui / test . txt ")) ĊĠ Ġ} ĊĊĠ Ġ@ Enable Auto Configuration ĊĠ Ġ@ Import ( Zip kin Ui Configuration :: class ) ĊĠ Ġclass ĠTest Server ĊĊĠ Ġprivate Ġfun Ġstring From Classpath ( path : ĠString ): ĠString Ġ{ ĊĠĠĠ Ġval Ġurl Ġ= Ġjava Class . class Loader . getResource ( path ) ĊĠĠĠ ĠassertThat ( url ). isNotNull () ĊĊĠĠĠ Ġurl !!. open Stream () ĊĠĠĠĠĠ Ġ. use Ġ{ Ġfrom Classpath Ġ-> Ġreturn ĠOk io . buffer ( Ok io . source ( from Classpath )). read Utf 8 () Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun ĠgetContent Encoding From Request That Accept s G zip ( path : ĠString ): ĠString ? Ġ{ ĊĠĠĠ Ġ// ĠWe Ġtypically Ġuse ĠOk Http Ġin Ġour Ġtests , Ġbut Ġthat Ġautomatically Ġunzip s .. ĊĠĠĠ Ġval Ġrequest Ġ= ĠRequest Headers . builder ( HttpMethod . GET , Ġpath ) ĊĠĠĠĠĠ Ġ. set ( Http Header Names . ACCEPT _ ENCODING , Ġ" gzip ") ĊĠĠĠĠĠ Ġ. build () ĊĠĠĠ Ġval Ġresponse Ġ= ĠHttpClient . of ( Http . url ( server , Ġ" ")). execute ( request ). aggregate (). join () ĊĠĠĠ Ġreturn Ġresponse . headers (). get ( Http Header Names . CONTENT _ ENCODING ) ĊĠ Ġ} Ċ } Ċ