11/*
22Copyright 2018 New Vector Ltd
3- Copyright 2019 The Matrix.org Foundation C.I.C.
3+ Copyright 2019, 2022 The Matrix.org Foundation C.I.C.
44
55Licensed under the Apache License, Version 2.0 (the "License");
66you may not use this file except in compliance with the License.
@@ -17,19 +17,20 @@ limitations under the License.
1717
1818import MockHttpBackend from "matrix-mock-request" ;
1919
20- import * as sdk from "../../src" ;
20+ import { request } from "../../src/matrix " ;
2121import { AutoDiscovery } from "../../src/autodiscovery" ;
2222
2323describe ( "AutoDiscovery" , function ( ) {
24- let httpBackend = null ;
25-
26- beforeEach ( function ( ) {
27- httpBackend = new MockHttpBackend ( ) ;
28- sdk . request ( httpBackend . requestFn ) ;
29- } ) ;
24+ const getHttpBackend = ( ) : MockHttpBackend => {
25+ const httpBackend = new MockHttpBackend ( ) ;
26+ request ( httpBackend . requestFn ) ;
27+ return httpBackend ;
28+ } ;
3029
3130 it ( "should throw an error when no domain is specified" , function ( ) {
31+ getHttpBackend ( ) ;
3232 return Promise . all ( [
33+ // @ts -ignore testing no args
3334 AutoDiscovery . findClientConfig ( /* no args */ ) . then ( ( ) => {
3435 throw new Error ( "Expected a failure, not success with no args" ) ;
3536 } , ( ) => {
@@ -42,13 +43,13 @@ describe("AutoDiscovery", function() {
4243 return true ;
4344 } ) ,
4445
45- AutoDiscovery . findClientConfig ( null ) . then ( ( ) => {
46+ AutoDiscovery . findClientConfig ( null as any ) . then ( ( ) => {
4647 throw new Error ( "Expected a failure, not success with null" ) ;
4748 } , ( ) => {
4849 return true ;
4950 } ) ,
5051
51- AutoDiscovery . findClientConfig ( true ) . then ( ( ) => {
52+ AutoDiscovery . findClientConfig ( true as any ) . then ( ( ) => {
5253 throw new Error ( "Expected a failure, not success with a non-string" ) ;
5354 } , ( ) => {
5455 return true ;
@@ -57,6 +58,7 @@ describe("AutoDiscovery", function() {
5758 } ) ;
5859
5960 it ( "should return PROMPT when .well-known 404s" , function ( ) {
61+ const httpBackend = getHttpBackend ( ) ;
6062 httpBackend . when ( "GET" , "/.well-known/matrix/client" ) . respond ( 404 , { } ) ;
6163 return Promise . all ( [
6264 httpBackend . flushAllExpected ( ) ,
@@ -80,6 +82,7 @@ describe("AutoDiscovery", function() {
8082 } ) ;
8183
8284 it ( "should return FAIL_PROMPT when .well-known returns a 500 error" , function ( ) {
85+ const httpBackend = getHttpBackend ( ) ;
8386 httpBackend . when ( "GET" , "/.well-known/matrix/client" ) . respond ( 500 , { } ) ;
8487 return Promise . all ( [
8588 httpBackend . flushAllExpected ( ) ,
@@ -103,6 +106,7 @@ describe("AutoDiscovery", function() {
103106 } ) ;
104107
105108 it ( "should return FAIL_PROMPT when .well-known returns a 400 error" , function ( ) {
109+ const httpBackend = getHttpBackend ( ) ;
106110 httpBackend . when ( "GET" , "/.well-known/matrix/client" ) . respond ( 400 , { } ) ;
107111 return Promise . all ( [
108112 httpBackend . flushAllExpected ( ) ,
@@ -126,6 +130,7 @@ describe("AutoDiscovery", function() {
126130 } ) ;
127131
128132 it ( "should return FAIL_PROMPT when .well-known returns an empty body" , function ( ) {
133+ const httpBackend = getHttpBackend ( ) ;
129134 httpBackend . when ( "GET" , "/.well-known/matrix/client" ) . respond ( 200 , "" ) ;
130135 return Promise . all ( [
131136 httpBackend . flushAllExpected ( ) ,
@@ -149,6 +154,7 @@ describe("AutoDiscovery", function() {
149154 } ) ;
150155
151156 it ( "should return FAIL_PROMPT when .well-known returns not-JSON" , function ( ) {
157+ const httpBackend = getHttpBackend ( ) ;
152158 httpBackend . when ( "GET" , "/.well-known/matrix/client" ) . respond ( 200 , "abc" ) ;
153159 return Promise . all ( [
154160 httpBackend . flushAllExpected ( ) ,
@@ -173,6 +179,7 @@ describe("AutoDiscovery", function() {
173179
174180 it ( "should return FAIL_PROMPT when .well-known does not have a base_url for " +
175181 "m.homeserver (empty string)" , function ( ) {
182+ const httpBackend = getHttpBackend ( ) ;
176183 httpBackend . when ( "GET" , "/.well-known/matrix/client" ) . respond ( 200 , {
177184 "m.homeserver" : {
178185 base_url : "" ,
@@ -201,6 +208,7 @@ describe("AutoDiscovery", function() {
201208
202209 it ( "should return FAIL_PROMPT when .well-known does not have a base_url for " +
203210 "m.homeserver (no property)" , function ( ) {
211+ const httpBackend = getHttpBackend ( ) ;
204212 httpBackend . when ( "GET" , "/.well-known/matrix/client" ) . respond ( 200 , {
205213 "m.homeserver" : { } ,
206214 } ) ;
@@ -227,6 +235,7 @@ describe("AutoDiscovery", function() {
227235
228236 it ( "should return FAIL_ERROR when .well-known has an invalid base_url for " +
229237 "m.homeserver (disallowed scheme)" , function ( ) {
238+ const httpBackend = getHttpBackend ( ) ;
230239 httpBackend . when ( "GET" , "/.well-known/matrix/client" ) . respond ( 200 , {
231240 "m.homeserver" : {
232241 base_url : "mxc://example.org" ,
@@ -255,6 +264,7 @@ describe("AutoDiscovery", function() {
255264
256265 it ( "should return FAIL_ERROR when .well-known has an invalid base_url for " +
257266 "m.homeserver (verification failure: 404)" , function ( ) {
267+ const httpBackend = getHttpBackend ( ) ;
258268 httpBackend . when ( "GET" , "/_matrix/client/versions" ) . respond ( 404 , { } ) ;
259269 httpBackend . when ( "GET" , "/.well-known/matrix/client" ) . respond ( 200 , {
260270 "m.homeserver" : {
@@ -284,6 +294,7 @@ describe("AutoDiscovery", function() {
284294
285295 it ( "should return FAIL_ERROR when .well-known has an invalid base_url for " +
286296 "m.homeserver (verification failure: 500)" , function ( ) {
297+ const httpBackend = getHttpBackend ( ) ;
287298 httpBackend . when ( "GET" , "/_matrix/client/versions" ) . respond ( 500 , { } ) ;
288299 httpBackend . when ( "GET" , "/.well-known/matrix/client" ) . respond ( 200 , {
289300 "m.homeserver" : {
@@ -313,6 +324,7 @@ describe("AutoDiscovery", function() {
313324
314325 it ( "should return FAIL_ERROR when .well-known has an invalid base_url for " +
315326 "m.homeserver (verification failure: 200 but wrong content)" , function ( ) {
327+ const httpBackend = getHttpBackend ( ) ;
316328 httpBackend . when ( "GET" , "/_matrix/client/versions" ) . respond ( 200 , {
317329 not_matrix_versions : [ "r0.0.1" ] ,
318330 } ) ;
@@ -344,8 +356,9 @@ describe("AutoDiscovery", function() {
344356
345357 it ( "should return SUCCESS when .well-known has a verifiably accurate base_url for " +
346358 "m.homeserver" , function ( ) {
359+ const httpBackend = getHttpBackend ( ) ;
347360 httpBackend . when ( "GET" , "/_matrix/client/versions" ) . check ( ( req ) => {
348- expect ( req . opts . uri ) . toEqual ( "https://example.org/_matrix/client/versions" ) ;
361+ expect ( req . path ) . toEqual ( "https://example.org/_matrix/client/versions" ) ;
349362 } ) . respond ( 200 , {
350363 versions : [ "r0.0.1" ] ,
351364 } ) ;
@@ -376,8 +389,9 @@ describe("AutoDiscovery", function() {
376389 } ) ;
377390
378391 it ( "should return SUCCESS with the right homeserver URL" , function ( ) {
392+ const httpBackend = getHttpBackend ( ) ;
379393 httpBackend . when ( "GET" , "/_matrix/client/versions" ) . check ( ( req ) => {
380- expect ( req . opts . uri )
394+ expect ( req . path )
381395 . toEqual ( "https://chat.example.org/_matrix/client/versions" ) ;
382396 } ) . respond ( 200 , {
383397 versions : [ "r0.0.1" ] ,
@@ -411,8 +425,9 @@ describe("AutoDiscovery", function() {
411425
412426 it ( "should return SUCCESS / FAIL_PROMPT when the identity server configuration " +
413427 "is wrong (missing base_url)" , function ( ) {
428+ const httpBackend = getHttpBackend ( ) ;
414429 httpBackend . when ( "GET" , "/_matrix/client/versions" ) . check ( ( req ) => {
415- expect ( req . opts . uri )
430+ expect ( req . path )
416431 . toEqual ( "https://chat.example.org/_matrix/client/versions" ) ;
417432 } ) . respond ( 200 , {
418433 versions : [ "r0.0.1" ] ,
@@ -451,8 +466,9 @@ describe("AutoDiscovery", function() {
451466
452467 it ( "should return SUCCESS / FAIL_PROMPT when the identity server configuration " +
453468 "is wrong (empty base_url)" , function ( ) {
469+ const httpBackend = getHttpBackend ( ) ;
454470 httpBackend . when ( "GET" , "/_matrix/client/versions" ) . check ( ( req ) => {
455- expect ( req . opts . uri )
471+ expect ( req . path )
456472 . toEqual ( "https://chat.example.org/_matrix/client/versions" ) ;
457473 } ) . respond ( 200 , {
458474 versions : [ "r0.0.1" ] ,
@@ -491,8 +507,9 @@ describe("AutoDiscovery", function() {
491507
492508 it ( "should return SUCCESS / FAIL_PROMPT when the identity server configuration " +
493509 "is wrong (validation error: 404)" , function ( ) {
510+ const httpBackend = getHttpBackend ( ) ;
494511 httpBackend . when ( "GET" , "/_matrix/client/versions" ) . check ( ( req ) => {
495- expect ( req . opts . uri )
512+ expect ( req . path )
496513 . toEqual ( "https://chat.example.org/_matrix/client/versions" ) ;
497514 } ) . respond ( 200 , {
498515 versions : [ "r0.0.1" ] ,
@@ -532,8 +549,9 @@ describe("AutoDiscovery", function() {
532549
533550 it ( "should return SUCCESS / FAIL_PROMPT when the identity server configuration " +
534551 "is wrong (validation error: 500)" , function ( ) {
552+ const httpBackend = getHttpBackend ( ) ;
535553 httpBackend . when ( "GET" , "/_matrix/client/versions" ) . check ( ( req ) => {
536- expect ( req . opts . uri )
554+ expect ( req . path )
537555 . toEqual ( "https://chat.example.org/_matrix/client/versions" ) ;
538556 } ) . respond ( 200 , {
539557 versions : [ "r0.0.1" ] ,
@@ -573,14 +591,15 @@ describe("AutoDiscovery", function() {
573591
574592 it ( "should return SUCCESS when the identity server configuration is " +
575593 "verifiably accurate" , function ( ) {
594+ const httpBackend = getHttpBackend ( ) ;
576595 httpBackend . when ( "GET" , "/_matrix/client/versions" ) . check ( ( req ) => {
577- expect ( req . opts . uri )
596+ expect ( req . path )
578597 . toEqual ( "https://chat.example.org/_matrix/client/versions" ) ;
579598 } ) . respond ( 200 , {
580599 versions : [ "r0.0.1" ] ,
581600 } ) ;
582601 httpBackend . when ( "GET" , "/_matrix/identity/api/v1" ) . check ( ( req ) => {
583- expect ( req . opts . uri )
602+ expect ( req . path )
584603 . toEqual ( "https://identity.example.org/_matrix/identity/api/v1" ) ;
585604 } ) . respond ( 200 , { } ) ;
586605 httpBackend . when ( "GET" , "/.well-known/matrix/client" ) . respond ( 200 , {
@@ -615,14 +634,15 @@ describe("AutoDiscovery", function() {
615634
616635 it ( "should return SUCCESS and preserve non-standard keys from the " +
617636 ".well-known response" , function ( ) {
637+ const httpBackend = getHttpBackend ( ) ;
618638 httpBackend . when ( "GET" , "/_matrix/client/versions" ) . check ( ( req ) => {
619- expect ( req . opts . uri )
639+ expect ( req . path )
620640 . toEqual ( "https://chat.example.org/_matrix/client/versions" ) ;
621641 } ) . respond ( 200 , {
622642 versions : [ "r0.0.1" ] ,
623643 } ) ;
624644 httpBackend . when ( "GET" , "/_matrix/identity/api/v1" ) . check ( ( req ) => {
625- expect ( req . opts . uri )
645+ expect ( req . path )
626646 . toEqual ( "https://identity.example.org/_matrix/identity/api/v1" ) ;
627647 } ) . respond ( 200 , { } ) ;
628648 httpBackend . when ( "GET" , "/.well-known/matrix/client" ) . respond ( 200 , {
0 commit comments