@@ -124,38 +124,73 @@ describe('mock server auth tests', () => {
124124 await conn . login ( ) . then ( ( key ) => expect ( key ) . toEqual ( apiKey ) ) ;
125125 } ) ;
126126
127- it ( 'should construct the correct baseUri for conn.http when host contains scheme' , async ( ) => {
127+ it ( 'should construct the correct url when host contains scheme' , ( ) => {
128128 const apiKey = 'abcd123' ;
129129
130130 const conn = new Connection ( {
131131 scheme : 'http' ,
132132 host : 'http://localhost:' + server . port ,
133133 apiKey : new ApiKey ( apiKey ) ,
134134 } ) ;
135+ const expectedPath = 'http://localhost:' + server . port ;
135136
136- await conn . http . get ( '/testEndpoint' ) ;
137- const lastRequestURL = server . lastRequest ( ) . path ; // Get the path of the last request
138- const expectedPath = '/v1/testEndpoint' ;
139- expect ( lastRequestURL ) . toEqual ( expectedPath ) ;
137+ expect ( conn . host ) . toEqual ( expectedPath ) ;
140138 } ) ;
141139
142- it ( 'should construct the correct baseUri for conn.query when host contains scheme' , async ( ) => {
140+ it ( 'should construct the correct url when scheme specified and host does not contain scheme' , ( ) => {
143141 const apiKey = 'abcd123' ;
144142
145143 const conn = new Connection ( {
146144 scheme : 'http' ,
145+ host : 'localhost:' + server . port ,
146+ apiKey : new ApiKey ( apiKey ) ,
147+ } ) ;
148+ const expectedPath = 'http://localhost:' + server . port ;
149+
150+ expect ( conn . host ) . toEqual ( expectedPath ) ;
151+ } ) ;
152+
153+ it ( 'should construct the correct url when no scheme is specified but host contains scheme' , ( ) => {
154+ const apiKey = 'abcd123' ;
155+
156+ const conn = new Connection ( {
147157 host : 'http://localhost:' + server . port ,
148158 apiKey : new ApiKey ( apiKey ) ,
149159 } ) ;
160+ const expectedPath = 'http://localhost:' + server . port ;
161+
162+ expect ( conn . host ) . toEqual ( expectedPath ) ;
163+ } ) ;
164+
165+ it ( 'should throw error when host contains different scheme than specified' , ( ) => {
166+ const apiKey = 'abcd123' ;
167+
168+ const createConnection = ( ) => {
169+ return new Connection ( {
170+ scheme : 'https' ,
171+ host : 'http://localhost:' + server . port ,
172+ apiKey : new ApiKey ( apiKey ) ,
173+ } ) ;
174+ } ;
175+
176+ expect ( createConnection ) . toThrow (
177+ 'The host contains a different protocol than specified in the scheme (scheme: https:// != host: http://)'
178+ ) ;
179+ } ) ;
180+
181+ it ( 'should throw error when scheme not specified and included in host' , ( ) => {
182+ const apiKey = 'abcd123' ;
183+
184+ const createConnection = ( ) => {
185+ return new Connection ( {
186+ host : 'localhost:' + server . port ,
187+ apiKey : new ApiKey ( apiKey ) ,
188+ } ) ;
189+ } ;
150190
151- try {
152- await conn . query ( '{ query { users } }' ) ;
153- } catch ( error ) {
154- // We just want to test the last request path
155- }
156- const lastRequestURL = server . lastRequest ( ) . path ;
157- const expectedPath = '/v1/graphql' ;
158- expect ( lastRequestURL ) . toEqual ( expectedPath ) ;
191+ expect ( createConnection ) . toThrow (
192+ 'The host must start with a recognized protocol (e.g., http:// or https://) if no scheme is provided.'
193+ ) ;
159194 } ) ;
160195
161196 it ( 'shuts down the server' , ( ) => {
0 commit comments