/* Ċ Ġ* ĠCopyright Ġ( C ) Ġ 2 0 2 0 ĠSquare , ĠInc . Ċ Ġ* Ċ Ġ* Ġ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 Ġokhttp 3 Ċ Ċ import Ġjavax . net . ssl . SSL Exception Ċ import Ġmock webserver 3 . Mock Response Ċ import Ġmock webserver 3 . Mock WebServer Ċ import Ġokhttp 3 . testing . Platform Rule Ċ import Ġokhttp 3 . tls . Handshake Certificates Ċ import Ġokhttp 3 . tls . Hel d Certificate Ċ import Ġokhttp 3 . tls . internal . Tls Util . localhost Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . junit . jupiter . api . extension . Register Extension Ċ import Ġorg . junit . jupiter . api . fail Ċ Ċ class ĠIn secure For Host Test ( ĊĠ Ġval Ġserver : ĠMock WebServer Ċ ) Ġ{ ĊĠ Ġ@ Register Extension Ġ@ Jvm Field Ġval Ġplatform Ġ= ĠPlatform Rule () ĊĠ Ġ@ Register Extension Ġ@ Jvm Field Ġval Ġclient Test Rule Ġ= ĠOk HttpClient Test Rule () ĊĊĠ Ġ@ Test Ġfun Ġ` un trusted Ġhost Ġin Ġin secure Hosts Ġconnects Ġsuccessfully `() Ġ{ ĊĠĠĠ Ġval Ġserver Certificates Ġ= Ġlocalhost () ĊĠĠĠ Ġserver . use Https ( server Certificates . ssl Socket Factory (), Ġfalse ) ĊĠĠĠ Ġserver . enqueue ( Mock Response ()) ĊĊĠĠĠ Ġval Ġclient Certificates Ġ= ĠHand shake Certificates . Builder () ĊĠĠĠĠĠĠĠ Ġ. add Platform Trusted Certificates () ĊĠĠĠĠĠĠĠ Ġ. add In secure Host ( server . host Name ) ĊĠĠĠĠĠĠĠ Ġ. build () ĊĊĠĠĠ Ġval Ġclient Ġ= Ġclient Test Rule . new Client Builder () ĊĠĠĠĠĠĠĠ Ġ. ssl Socket Factory ( client Certificates . ssl Socket Factory (), Ġclient Certificates . trust Manager ) ĊĠĠĠĠĠĠĠ Ġ. build () ĊĊĠĠĠ Ġval Ġcall Ġ= Ġclient . new Call ( Request . Builder () ĊĠĠĠĠĠĠĠ Ġ. url ( server . url ("/ ")) ĊĠĠĠĠĠĠĠ Ġ. build ()) ĊĠĠĠ Ġval Ġresponse Ġ= Ġcall . execute () ĊĠĠĠ ĠassertThat ( response . code ). isEqualTo ( 2 0 0 ) ĊĠĠĠ ĠassertThat ( response . handshake !!. cipher Suite ). isNotNull () ĊĠĠĠ ĠassertThat ( response . handshake !!. tls Version ). isNotNull () ĊĠĠĠ ĠassertThat ( response . handshake !!. local Certificates ). isEmpty () ĊĠĠĠ ĠassertThat ( response . handshake !!. local Principal ). isNull () ĊĠĠĠ ĠassertThat ( response . handshake !!. peer Certificates ). isEmpty () ĊĠĠĠ ĠassertThat ( response . handshake !!. peer Principal ). isNull () ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġ` bad Ġcertificates Ġhost Ġin Ġin secure Hosts Ġfails Ġwith ĠSSL Exception `() Ġ{ ĊĠĠĠ Ġval Ġheld Certificate Ġ= ĠHel d Certificate . Builder () ĊĠĠĠĠĠĠĠ Ġ. add Subject Alternative Name (" example . com ") ĊĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠ Ġval Ġserver Certificates Ġ= ĠHand shake Certificates . Builder () ĊĠĠĠĠĠĠĠ Ġ. held Certificate ( held Certificate ) ĊĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠ Ġserver . use Https ( server Certificates . ssl Socket Factory (), Ġfalse ) ĊĠĠĠ Ġserver . enqueue ( Mock Response ()) ĊĊĠĠĠ Ġval Ġclient Certificates Ġ= ĠHand shake Certificates . Builder () ĊĠĠĠĠĠĠĠ Ġ. add Platform Trusted Certificates () ĊĠĠĠĠĠĠĠ Ġ. add In secure Host ( server . host Name ) ĊĠĠĠĠĠĠĠ Ġ. build () ĊĊĠĠĠ Ġval Ġclient Ġ= Ġclient Test Rule . new Client Builder () ĊĠĠĠĠĠĠĠ Ġ. ssl Socket Factory ( client Certificates . ssl Socket Factory (), Ġclient Certificates . trust Manager ) ĊĠĠĠĠĠĠĠ Ġ. build () ĊĊĠĠĠ Ġval Ġcall Ġ= Ġclient . new Call ( Request . Builder () ĊĠĠĠĠĠĠĠ Ġ. url ( server . url ("/ ")) ĊĠĠĠĠĠĠĠ Ġ. build ()) ĊĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠ Ġcall . execute () ĊĠĠĠĠĠ Ġfail ("") ĊĠĠĠ Ġ} Ġcatch Ġ( expected : ĠSSL Exception ) Ġ{ ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġ@ Test Ġfun Ġ` un trusted Ġhost Ġnot Ġin Ġin secure Hosts Ġfails Ġwith ĠSSL Exception `() Ġ{ ĊĠĠĠ Ġval Ġserver Certificates Ġ= Ġlocalhost () ĊĠĠĠ Ġserver . use Https ( server Certificates . ssl Socket Factory (), Ġfalse ) ĊĠĠĠ Ġserver . enqueue ( Mock Response ()) ĊĊĠĠĠ Ġval Ġclient Certificates Ġ= ĠHand shake Certificates . Builder () ĊĠĠĠĠĠĠĠ Ġ. add Platform Trusted Certificates () ĊĠĠĠĠĠĠĠ Ġ. add In secure Host ("${ server . host Name } 2 ") ĊĠĠĠĠĠĠĠ Ġ. build () ĊĊĠĠĠ Ġval Ġclient Ġ= Ġclient Test Rule . new Client Builder () ĊĠĠĠĠĠĠĠ Ġ. ssl Socket Factory ( client Certificates . ssl Socket Factory (), Ġclient Certificates . trust Manager ) ĊĠĠĠĠĠĠĠ Ġ. build () ĊĊĠĠĠ Ġval Ġcall Ġ= Ġclient . new Call ( Request . Builder () ĊĠĠĠĠĠĠĠ Ġ. url ( server . url ("/ ")) ĊĠĠĠĠĠĠĠ Ġ. build ()) ĊĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠ Ġcall . execute () ĊĠĠĠĠĠ Ġfail ("") ĊĠĠĠ Ġ} Ġcatch Ġ( expected : ĠSSL Exception ) Ġ{ ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ