Skip to content

Commit 90b754f

Browse files
author
Peter Bengtsson
authored
Test JSON API without enabling dedicated search results page (github#30542)
1 parent 9e302ad commit 90b754f

File tree

6 files changed

+21
-8
lines changed

6 files changed

+21
-8
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ env:
2323
# Setting this will activate the jest tests that depend on actually
2424
# sending real search queries to Elasticsearch
2525
ELASTICSEARCH_URL: http://localhost:9200/
26+
# Hopefully the name is clear enough. By enabling this, we're testing
27+
# the future code.
28+
ENABLE_SEARCH_RESULTS_PAGE: true
2629

2730
jobs:
2831
test:

middleware/redirects/handle-redirects.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,31 @@ export default function handleRedirects(req, res, next) {
3939
let redirect = req.path
4040
let queryParams = req._parsedUrl.query
4141

42-
// If process.env.ELASTICSEARCH_URL isn't set, you can't go to the
42+
// If process.env.ENABLE_SEARCH_RESULTS_PAGE isn't set, you can't go to the
4343
// dedicated search results page.
4444
// If that's the case, use the "old redirect" where all it does is
4545
// "correcting" the old query string 'q' to 'query'.
46-
if (!process.env.ELASTICSEARCH_URL && 'q' in req.query && !('query' in req.query)) {
46+
if (!process.env.ENABLE_SEARCH_RESULTS_PAGE && 'q' in req.query && !('query' in req.query)) {
4747
// update old-style query params (#9467)
4848
const newQueryParams = new URLSearchParams(queryParams)
4949
newQueryParams.set('query', newQueryParams.get('q'))
5050
newQueryParams.delete('q')
5151
return res.redirect(301, `${req.path}?${newQueryParams.toString()}`)
5252
}
5353

54-
// If process.env.ELASTICSEARCH_URL is set, the dedicated search
54+
// If process.env.ENABLE_SEARCH_RESULTS_PAGE is set, the dedicated search
5555
// result page is ready. If that's the case, we can redirect to
5656
// `/$locale/search?query=...` from `/foo/bar?query=...` or from
5757
// (the old style) `/foo/bar/?q=...`
5858
if (
59-
process.env.ELASTICSEARCH_URL &&
59+
process.env.ENABLE_SEARCH_RESULTS_PAGE &&
6060
('q' in req.query ||
6161
('query' in req.query &&
6262
!(req.path.endsWith('/search') || req.path.startsWith('/api/search'))))
6363
) {
6464
// If you had the old legacy format of /some/uri?q=stuff
6565
// it needs to redirect to /en/search?query=stuff or
66-
// /some/uri?query=stuff depending on if ELASTICSEARCH_URL has been
66+
// /some/uri?query=stuff depending on if ENABLE_SEARCH_RESULTS_PAGE has been
6767
// set up.
6868
// If you have the new format of /some/uri?query=stuff it too needs
6969
// to redirect to /en/search?query=stuff

pages/search.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
2626
// So if that's the case, which might be true in production (Aug 2022)
2727
// or on an engineers local development, we basically pretend the
2828
// page doesn't exist.
29-
if (!process.env.ELASTICSEARCH_URL) {
29+
// By depending conditionally on these two environment variables we're
30+
// able to carefully launch the dedicated search results page
31+
// separately from the JSON API endpoint.
32+
if (!process.env.ELASTICSEARCH_URL || !process.env.ENABLE_SEARCH_RESULTS_PAGE) {
3033
return { notFound: true }
3134
}
3235

tests/helpers/conditional-runs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ const runningActionsOnInternalRepo =
44
export const testViaActionsOnly = runningActionsOnInternalRepo ? test : test.skip
55
export const describeViaActionsOnly = runningActionsOnInternalRepo ? describe : describe.skip
66
export const describeIfElasticsearchURL = process.env.ELASTICSEARCH_URL ? describe : describe.skip
7+
export const describeIfDedicatedSearchResultsPage = process.env.ENABLE_SEARCH_RESULTS_PAGE
8+
? describe
9+
: describe.skip
710

811
export default {
912
testViaActionsOnly,
1013
describeViaActionsOnly,
1114
describeIfElasticsearchURL,
15+
describeIfDedicatedSearchResultsPage,
1216
}

tests/rendering/search.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { expect, jest } from '@jest/globals'
22

33
import { getDOM } from '../helpers/e2etest.js'
4+
import { describeIfDedicatedSearchResultsPage } from '../helpers/conditional-runs.js'
45

5-
describe('search results page', () => {
6+
describeIfDedicatedSearchResultsPage('search results page', () => {
67
jest.setTimeout(5 * 60 * 1000)
78

89
test('says something if no query is provided', async () => {

tests/routing/redirects.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
1616
// dedicated search results page works.
1717
// In a near future, we won't be needing this and assume it's always
1818
// true.
19-
const USE_DEDICATED_SEARCH_RESULTS_PAGE = Boolean(process.env.ELASTICSEARCH_URL)
19+
const USE_DEDICATED_SEARCH_RESULTS_PAGE = Boolean(
20+
JSON.parse(process.env.ENABLE_SEARCH_RESULTS_PAGE || 'false')
21+
)
2022

2123
describe('redirects', () => {
2224
jest.setTimeout(5 * 60 * 1000)

0 commit comments

Comments
 (0)