-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(KUI-1369): noIndexMiddleware and noIndexMiddleware.test has been added to make sure that URL with aap*. is not indexed by google #357
Merged
amirhossein-haerian
merged 2 commits into
master
from
issues/KUI-1369-not-indexed-by-google
Jun 25, 2024
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const { server: serverConfig } = require('../configuration') | ||
|
||
/** | ||
* Middleware that adds "noindex" robots header if "server host" and the request | ||
* host ("forwarded host") doesn't match. This is done to prevent Google from | ||
* indexing app.kth.se/* when we want the user to use www.kth.se/* | ||
* | ||
* The "server host" (SERVER_HOST_URL) is the primary host we expected the | ||
* app to be hosted on (ie https://www.kth.se). | ||
* | ||
* The orginal host of the request is replaced with an Azure host | ||
* (*.azurewebsites.net) before reaching our app so we look at the "forwarded | ||
* host" ("x-forwarded-host" header) . | ||
* | ||
* Default, if x-forwarded-host is missing, is to do nothing. | ||
*/ | ||
const noIndexMiddleware = function (req, res, next) { | ||
const forwardedHost = req.header('x-forwarded-host') | ||
if (forwardedHost) { | ||
const serverHostUrl = new URL(serverConfig.hostUrl) | ||
const serverHost = serverHostUrl.host | ||
if (serverHost !== forwardedHost) { | ||
res.set('x-robots-tag', 'noindex') | ||
} | ||
} | ||
next() | ||
} | ||
module.exports = noIndexMiddleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const httpMocks = require('node-mocks-http') | ||
|
||
const noIndexMiddleware = require('./noIndexMiddleware') | ||
|
||
const mockedServerHost = 'mockedserverhost.example.com' | ||
jest.mock('../configuration', () => ({ | ||
server: { hostUrl: 'https://mockedserverhost.example.com' }, | ||
})) | ||
|
||
describe('noIndexMiddleware', () => { | ||
it('should not set "X-Robots-Tag" header if "X-Forwarded-Host" is missing', () => { | ||
const next = jest.fn() | ||
const { req, res } = httpMocks.createMocks({ | ||
headers: {}, | ||
}) | ||
|
||
noIndexMiddleware(req, res, next) | ||
|
||
expect(res.getHeader('X-Robots-Tag')).toBeUndefined() | ||
expect(next).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should not set "X-Robots-Tag" header if "X-Forwarded-Host" match SERVER_HOST_URL', () => { | ||
const next = jest.fn() | ||
const { req, res } = httpMocks.createMocks({ | ||
headers: { | ||
'X-Forwarded-Host': mockedServerHost, | ||
}, | ||
}) | ||
|
||
noIndexMiddleware(req, res, next) | ||
|
||
expect(res.getHeader('X-Robots-Tag')).toBeUndefined() | ||
expect(next).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should set "X-Robots-Tag" header if "X-Forwarded-Host" does not match SERVER_HOST_URL', () => { | ||
const next = jest.fn() | ||
const { req, res } = httpMocks.createMocks({ | ||
headers: { | ||
'X-Forwarded-Host': 'anotherhost.example.com', | ||
}, | ||
}) | ||
|
||
noIndexMiddleware(req, res, next) | ||
|
||
expect(res.getHeader('X-Robots-Tag')).toBe('noindex') | ||
expect(next).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good solution. The reason why this works in KP, is that we there use
jest-extended
In KP's jest.config.js:
setupFilesAfterEnv: ['jest-extended/all'],
If you want, you can also include
jest-extended
in kursinfo-web and include it as above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sharing this, I have added the est-extended package and the required config. I also changed "toHaveBeenCalledTimes()" to "toHaveBeenCalledOnce()".