Skip to content

Commit bd0eea3

Browse files
authored
Add headers tests (#1127)
* Add headers tests * Test unknown header * Add tests to guarantee different headers are accepted (#1128) * Add meilisearch downloader * Revert CI changes * Add browser test in pre release tests
1 parent 171ac47 commit bd0eea3

File tree

6 files changed

+87
-24
lines changed

6 files changed

+87
-24
lines changed

.github/workflows/pre-release-tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ jobs:
4545
run: yarn test:env:nodejs
4646
- name: Run node typescript env
4747
run: yarn test:env:node-ts
48+
- name: Run Browser env
49+
run: yarn test:env:browser
4850

4951
linter_check:
5052
runs-on: ubuntu-latest

tests/env/browser/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// also works with window.MeiliSearch
2323

2424
const client = new window.MeiliSearch({
25-
host: 'http://127.0.0.1:7700',
25+
host: 'http://localhost:7700',
2626
apiKey: 'masterKey',
2727
})
2828
const task = await client.createIndex(UID)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset='utf-8'>
6+
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
7+
<title>Page Title</title>
8+
<meta name='viewport' content='width=device-width, initial-scale=1'>
9+
</head>
10+
11+
<body>
12+
<div id="content"></div>
13+
</body>
14+
15+
</html>
16+
<script src="./meilisearch.umd.js"></script>
17+
<script>
18+
; (async () => {
19+
20+
const errorDiv = document.createElement("div");
21+
errorDiv.setAttribute("id", "error");
22+
23+
let error = 'NO ERRORS'
24+
try {
25+
const task = await client.createIndex(UID)
26+
await client.waitForTask(task.uid)
27+
await fetch('http://localhost:7700/indexes/movies/documents', {
28+
method: 'POST',
29+
headers: {
30+
Authorization: `Bearer masterKey`,
31+
'User-Agent': 'plif plouf',
32+
"wrong-header": "wrong header"
33+
},
34+
body: []
35+
})
36+
} catch (e) {
37+
error = e.message
38+
}
39+
40+
errorDiv.innerHTML = error
41+
document.body.insertBefore(errorDiv, document.querySelector("#content"));
42+
43+
const deleteTask = await client.index(UID).delete()
44+
await client.waitForTask(deleteTask.uid)
45+
})()
46+
47+
48+
</script>

tests/env/express/public/index.html

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
<script src="./meilisearch.umd.js"></script>
1818
<script>
1919
; (async () => {
20+
let content = ''
21+
let searchDiv = undefined
22+
// also works with window.MeiliSearch
23+
const client = new MeiliSearch({
24+
host: 'http://127.0.0.1:7700',
25+
apiKey: 'masterKey',
26+
})
27+
const UID = "testIndex"
2028

2129
try {
22-
const UID = "testIndex"
23-
24-
// also works with window.MeiliSearch
25-
const client = new MeiliSearch({
26-
host: 'http://127.0.0.1:7700',
27-
apiKey: 'masterKey',
28-
})
2930
const task = await client.createIndex(UID)
3031
await client.waitForTask(task.uid)
3132

@@ -40,17 +41,18 @@
4041
indexDiv.innerHTML = index.uid
4142
document.body.insertBefore(indexDiv, document.querySelector("#content"));
4243

43-
const searchDiv = document.createElement("div");
44+
searchDiv = document.createElement("div");
4445
searchDiv.setAttribute("id", "search");
4546
const search = await client.index(UID).search()
46-
searchDiv.innerHTML = JSON.stringify(search)
47-
document.body.insertBefore(searchDiv, document.querySelector("#content"));
48-
49-
const deleteTask = await client.index(UID).delete()
50-
await client.waitForTask(deleteTask.uid)
47+
content = JSON.stringify(search)
5148
} catch (e) {
5249
console.error(e);
50+
content = e.message
5351
}
52+
searchDiv.innerHTML = content
53+
document.body.insertBefore(searchDiv, document.querySelector("#content"));
54+
const deleteTask = await client.index(UID).delete()
55+
await client.waitForTask(deleteTask.uid)
5456
})()
5557

5658

tests/env/express/src/server.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ const app = express()
33
const router = express.Router()
44

55
console.log(process.cwd())
6-
router.get('/', function (req, res) {
6+
router.get('/meilisearch', function (req, res) {
77
res.sendFile(`${process.cwd()}/public/index.html`)
88
})
9+
10+
router.get('/headers', function (req, res) {
11+
res.sendFile(`${process.cwd()}/public/headers.html`)
12+
})
913
app.use(express.static('public'))
14+
1015
// add the router
1116
app.use('/', router)
1217
app.listen(process.env.port || 3000)
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
describe('MeiliSearch JS Browser test', () => {
22
beforeAll(async () => {
3-
await page.goto('http://localhost:3000')
3+
await page.goto('http://localhost:3000/meilisearch')
44
})
55

66
it('Should have created an index and displayed it', async () => {
77
await page.waitForSelector("#indexes")
8-
await expect(
9-
page.content()
10-
).resolves.toMatch('testIndex')
8+
let element = await page.$('#indexes')
9+
let value = await page.evaluate(el => el.textContent, element)
10+
await expect(value).toMatch('testIndex')
1111
})
12+
})
1213

13-
it('Should have successfully searched on the index', async () => {
14-
await page.waitForSelector("#indexes")
15-
await expect(
16-
page.content()
17-
).resolves.toMatch('wonder woman')
14+
describe('MeiliSearch JS Browser test', () => {
15+
beforeAll(async () => {
16+
await page.goto('http://localhost:3000/headers')
17+
})
18+
it('Should not throw cors error', async () => {
19+
await page.waitForSelector("#error")
20+
let element = await page.$('#error')
21+
let value = await page.evaluate(el => el.textContent, element)
22+
await expect(value).toMatch('NO ERRORS')
1823
})
24+
1925
})

0 commit comments

Comments
 (0)