Skip to content

Commit 85f0973

Browse files
bidoubiwacurquiza
andauthored
Add tests on each route to ensure avoiding double slashes (#732)
* Add tests on each route to confirm no double slashes * Remove duplicated test * Remove duplicated test * Update tests/client_tests.ts Co-authored-by: Clémentine Urquizar <[email protected]> * Refactor URL construction * Add tests on each route to confirm no double slashes * Remove duplicated test * Remove duplicated test * Update tests/client_tests.ts Co-authored-by: Clémentine Urquizar <[email protected]> * Add unit tests on routes construction * Update client tests to match new slash handling Co-authored-by: Clémentine Urquizar <[email protected]>
1 parent 6f6f320 commit 85f0973

17 files changed

+751
-11
lines changed

tests/attributes_for_faceting_tests.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
privateClient,
77
publicClient,
88
anonymousClient,
9+
badHostClient,
10+
BAD_HOST,
911
} from './meilisearch-test-utils'
1012

1113
const index = {
@@ -145,3 +147,52 @@ describe.each([{ client: anonymousClient, permission: 'No' }])(
145147
})
146148
}
147149
)
150+
151+
test(`Get request should not add double slash nor a trailing slash`, async () => {
152+
try {
153+
const res = await badHostClient.index(index.uid).getAttributesForFaceting()
154+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
155+
} catch (e) {
156+
expect(e.message).toMatch(
157+
`${BAD_HOST}/indexes/movies_test/settings/attributes-for-faceting`
158+
)
159+
expect(e.message).not.toMatch(
160+
`${BAD_HOST}/indexes/movies_test/settings/attributes-for-faceting/`
161+
)
162+
expect(e.type).toBe('MeiliSearchCommunicationError')
163+
}
164+
})
165+
166+
test(`Update request should not add double slash nor a trailing slash`, async () => {
167+
try {
168+
const res = await badHostClient
169+
.index(index.uid)
170+
.updateAttributesForFaceting([])
171+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
172+
} catch (e) {
173+
expect(e.message).toMatch(
174+
`${BAD_HOST}/indexes/movies_test/settings/attributes-for-faceting`
175+
)
176+
expect(e.message).not.toMatch(
177+
`${BAD_HOST}/indexes/movies_test/settings/attributes-for-faceting/`
178+
)
179+
expect(e.type).toBe('MeiliSearchCommunicationError')
180+
}
181+
})
182+
183+
test(`Reset request should not add double slash nor a trailing slash`, async () => {
184+
try {
185+
const res = await badHostClient
186+
.index(index.uid)
187+
.resetAttributesForFaceting()
188+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
189+
} catch (e) {
190+
expect(e.message).toMatch(
191+
`${BAD_HOST}/indexes/movies_test/settings/attributes-for-faceting`
192+
)
193+
expect(e.message).not.toMatch(
194+
`${BAD_HOST}/indexes/movies_test/settings/attributes-for-faceting/`
195+
)
196+
expect(e.type).toBe('MeiliSearchCommunicationError')
197+
}
198+
})

tests/client_tests.ts

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
MeiliSearch,
66
MASTER_KEY,
77
PRIVATE_KEY,
8+
BAD_HOST,
89
} from './meilisearch-test-utils'
910

1011
afterAll(() => {
@@ -38,28 +39,63 @@ describe.each([
3839
expect(health).toBe(true)
3940
})
4041

41-
test(`${permission} key: Client handles host URL with domain and path`, () => {
42-
const customHost = `${config.host}/api`
43-
const client = new MeiliSearch({
44-
host: customHost,
45-
apiKey: key,
46-
})
47-
expect(client.config.host).toBe(customHost)
48-
expect(client.httpRequest.url).toBe(customHost)
42+
test(`${permission} key: No double slash when on host with domain and path and trailing slash`, async () => {
43+
try {
44+
const customHost = `${BAD_HOST}/api/`
45+
const client = new MeiliSearch({
46+
host: customHost,
47+
apiKey: key,
48+
})
49+
const health = await client.isHealthy()
50+
expect(health).toBe(false) // Left here to trigger failed test if error is not thrown
51+
} catch (e) {
52+
expect(e.message).toMatch(`${BAD_HOST}/api/health`)
53+
expect(e.type).toBe('MeiliSearchCommunicationError')
54+
}
55+
})
56+
57+
test(`${permission} key: No double slash when on host with domain and path and no trailing slash`, async () => {
58+
try {
59+
const customHost = `${BAD_HOST}/api`
60+
const client = new MeiliSearch({
61+
host: customHost,
62+
apiKey: key,
63+
})
64+
const health = await client.isHealthy()
65+
expect(health).toBe(false) // Left here to trigger failed test if error is not thrown
66+
} catch (e) {
67+
expect(e.message).toMatch(`${BAD_HOST}/api/health`)
68+
expect(e.type).toBe('MeiliSearchCommunicationError')
69+
}
70+
})
71+
72+
test(`${permission} key: host with double slash should keep double slash`, async () => {
73+
try {
74+
const customHost = `${BAD_HOST}//`
75+
const client = new MeiliSearch({
76+
host: customHost,
77+
apiKey: key,
78+
})
79+
const health = await client.isHealthy()
80+
expect(health).toBe(false) // Left here to trigger failed test if error is not thrown
81+
} catch (e) {
82+
expect(e.message).toMatch(`${BAD_HOST}//health`)
83+
expect(e.type).toBe('MeiliSearchCommunicationError')
84+
}
4985
})
5086

51-
test(`${permission} key: Client uses complete URL with domain and additionnal path in MeiliSearch call`, async () => {
87+
test(`${permission} key: host with one slash should not double slash`, async () => {
5288
try {
53-
const customHost = `${config.host}/api`
89+
const customHost = `${BAD_HOST}/`
5490
const client = new MeiliSearch({
5591
host: customHost,
5692
apiKey: key,
5793
})
5894
const health = await client.isHealthy()
5995
expect(health).toBe(false) // Left here to trigger failed test if error is not thrown
6096
} catch (e) {
97+
expect(e.message).toMatch(`${BAD_HOST}/health`)
6198
expect(e.type).toBe('MeiliSearchCommunicationError')
62-
expect(e.message).toBe('Not Found') // Expect 404 because host does not exist
6399
}
64100
})
65101
})

tests/displayed_attributes_tests.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
privateClient,
77
publicClient,
88
anonymousClient,
9+
badHostClient,
10+
BAD_HOST,
911
} from './meilisearch-test-utils'
1012

1113
const index = {
@@ -145,3 +147,50 @@ describe.each([{ client: anonymousClient, permission: 'No' }])(
145147
})
146148
}
147149
)
150+
151+
test(`Get request should not add double slash nor a trailing slash`, async () => {
152+
try {
153+
const res = await badHostClient.index(index.uid).getDisplayedAttributes()
154+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
155+
} catch (e) {
156+
expect(e.message).toMatch(
157+
`${BAD_HOST}/indexes/movies_test/settings/displayed-attributes`
158+
)
159+
expect(e.message).not.toMatch(
160+
`${BAD_HOST}/indexes/movies_test/settings/displayed-attributes/`
161+
)
162+
expect(e.type).toBe('MeiliSearchCommunicationError')
163+
}
164+
})
165+
166+
test(`Update request should not add double slash nor a trailing slash`, async () => {
167+
try {
168+
const res = await badHostClient
169+
.index(index.uid)
170+
.updateDisplayedAttributes([])
171+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
172+
} catch (e) {
173+
expect(e.message).toMatch(
174+
`${BAD_HOST}/indexes/movies_test/settings/displayed-attributes`
175+
)
176+
expect(e.message).not.toMatch(
177+
`${BAD_HOST}/indexes/movies_test/settings/displayed-attributes/`
178+
)
179+
expect(e.type).toBe('MeiliSearchCommunicationError')
180+
}
181+
})
182+
183+
test(`Reset request should not add double slash nor a trailing slash`, async () => {
184+
try {
185+
const res = await badHostClient.index(index.uid).resetDisplayedAttributes()
186+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
187+
} catch (e) {
188+
expect(e.message).toMatch(
189+
`${BAD_HOST}/indexes/movies_test/settings/displayed-attributes`
190+
)
191+
expect(e.message).not.toMatch(
192+
`${BAD_HOST}/indexes/movies_test/settings/displayed-attributes/`
193+
)
194+
expect(e.type).toBe('MeiliSearchCommunicationError')
195+
}
196+
})

tests/distinct_attribute_tests.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
privateClient,
77
publicClient,
88
anonymousClient,
9+
badHostClient,
10+
BAD_HOST,
911
} from './meilisearch-test-utils'
1012

1113
const index = {
@@ -145,3 +147,48 @@ describe.each([{ client: anonymousClient, permission: 'No' }])(
145147
})
146148
}
147149
)
150+
151+
test(`Get request should not add double slash nor a trailing slash`, async () => {
152+
try {
153+
const res = await badHostClient.index(index.uid).getDistinctAttribute()
154+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
155+
} catch (e) {
156+
expect(e.message).toMatch(
157+
`${BAD_HOST}/indexes/movies_test/settings/distinct-attribute`
158+
)
159+
expect(e.message).not.toMatch(
160+
`${BAD_HOST}/indexes/movies_test/settings/distinct-attribute/`
161+
)
162+
expect(e.type).toBe('MeiliSearchCommunicationError')
163+
}
164+
})
165+
166+
test(`Update request should not add double slash nor a trailing slash`, async () => {
167+
try {
168+
const res = await badHostClient.index(index.uid).updateDistinctAttribute('')
169+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
170+
} catch (e) {
171+
expect(e.message).toMatch(
172+
`${BAD_HOST}/indexes/movies_test/settings/distinct-attribute`
173+
)
174+
expect(e.message).not.toMatch(
175+
`${BAD_HOST}/indexes/movies_test/settings/distinct-attribute/`
176+
)
177+
expect(e.type).toBe('MeiliSearchCommunicationError')
178+
}
179+
})
180+
181+
test(`Reset request should not add double slash nor a trailing slash`, async () => {
182+
try {
183+
const res = await badHostClient.index(index.uid).resetDistinctAttribute()
184+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
185+
} catch (e) {
186+
expect(e.message).toMatch(
187+
`${BAD_HOST}/indexes/movies_test/settings/distinct-attribute`
188+
)
189+
expect(e.message).not.toMatch(
190+
`${BAD_HOST}/indexes/movies_test/settings/distinct-attribute/`
191+
)
192+
expect(e.type).toBe('MeiliSearchCommunicationError')
193+
}
194+
})

tests/documents_tests.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
privateClient,
77
publicClient,
88
anonymousClient,
9+
badHostClient,
10+
BAD_HOST,
911
} from './meilisearch-test-utils'
1012

1113
const uidNoPrimaryKey = {
@@ -499,3 +501,98 @@ describe.each([{ client: anonymousClient, permission: 'No' }])(
499501
})
500502
}
501503
)
504+
505+
test(`Get request should not add double slash nor a trailing slash`, async () => {
506+
try {
507+
const res = await badHostClient.index(uidNoPrimaryKey.uid).getDocuments()
508+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
509+
} catch (e) {
510+
expect(e.message).toMatch(`${BAD_HOST}/indexes/movies_test/documents`)
511+
expect(e.message).not.toMatch(`${BAD_HOST}/indexes/movies_test/documents/`)
512+
expect(e.type).toBe('MeiliSearchCommunicationError')
513+
}
514+
})
515+
516+
test(`Get request with options should not add double slash nor a trailing slash`, async () => {
517+
try {
518+
const res = await badHostClient
519+
.index(uidNoPrimaryKey.uid)
520+
.getDocuments({ attributesToRetrieve: ['id'] })
521+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
522+
} catch (e) {
523+
expect(e.message).toMatch(
524+
`${BAD_HOST}/indexes/movies_test/documents?attributesToRetrieve=id`
525+
)
526+
expect(e.message).not.toMatch(`${BAD_HOST}/indexes/movies_test/documents/`)
527+
expect(e.type).toBe('MeiliSearchCommunicationError')
528+
}
529+
})
530+
531+
test(`Update request should not add double slash nor a trailing slash`, async () => {
532+
try {
533+
const res = await badHostClient
534+
.index(uidNoPrimaryKey.uid)
535+
.updateDocuments([])
536+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
537+
} catch (e) {
538+
expect(e.message).toMatch(`${BAD_HOST}/indexes/movies_test/documents`)
539+
expect(e.message).not.toMatch(`${BAD_HOST}/indexes/movies_test/documents/`)
540+
expect(e.type).toBe('MeiliSearchCommunicationError')
541+
}
542+
})
543+
544+
test(`Delete batch request should not add double slash nor a trailing slash`, async () => {
545+
try {
546+
const res = await badHostClient
547+
.index(uidNoPrimaryKey.uid)
548+
.deleteDocuments([])
549+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
550+
} catch (e) {
551+
expect(e.message).toMatch(
552+
`${BAD_HOST}/indexes/movies_test/documents/delete-batch`
553+
)
554+
expect(e.message).not.toMatch(
555+
`${BAD_HOST}/indexes/movies_test/documents/delete-batch/`
556+
)
557+
expect(e.type).toBe('MeiliSearchCommunicationError')
558+
}
559+
})
560+
561+
test(`Delete all request should not add double slash nor a trailing slash`, async () => {
562+
try {
563+
const res = await badHostClient
564+
.index(uidNoPrimaryKey.uid)
565+
.deleteAllDocuments()
566+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
567+
} catch (e) {
568+
expect(e.message).toMatch(`${BAD_HOST}/indexes/movies_test/documents`)
569+
expect(e.message).not.toMatch(`${BAD_HOST}/indexes/movies_test/documents/`)
570+
expect(e.type).toBe('MeiliSearchCommunicationError')
571+
}
572+
})
573+
574+
test(`Delete one request should not add double slash nor a trailing slash`, async () => {
575+
try {
576+
const res = await badHostClient.index(uidNoPrimaryKey.uid).deleteDocument(1)
577+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
578+
} catch (e) {
579+
expect(e.message).toMatch(`${BAD_HOST}/indexes/movies_test/documents/1`)
580+
expect(e.message).not.toMatch(
581+
`${BAD_HOST}/indexes/movies_test/documents/1/`
582+
)
583+
expect(e.type).toBe('MeiliSearchCommunicationError')
584+
}
585+
})
586+
587+
test(`Get one request should not add double slash nor a trailing slash`, async () => {
588+
try {
589+
const res = await badHostClient.index(uidNoPrimaryKey.uid).getDocument(1)
590+
expect(res).toBe(undefined) // Left here to trigger failed test if error is not thrown
591+
} catch (e) {
592+
expect(e.message).toMatch(`${BAD_HOST}/indexes/movies_test/documents/1`)
593+
expect(e.message).not.toMatch(
594+
`${BAD_HOST}/indexes/movies_test/documents/1/`
595+
)
596+
expect(e.type).toBe('MeiliSearchCommunicationError')
597+
}
598+
})

0 commit comments

Comments
 (0)