-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from ONSdigital/848-browser-caching
Log when client and API versions are different
- Loading branch information
Showing
14 changed files
with
160 additions
and
35 deletions.
There are no files selected for viewing
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,16 @@ | ||
module.exports = (req, res, next) => { | ||
if ( | ||
req.headers.clientversion && | ||
process.env.EQ_AUTHOR_API_VERSION && | ||
req.headers.clientversion !== process.env.EQ_AUTHOR_API_VERSION | ||
) { | ||
req.log.warn( | ||
{ | ||
clientversion: req.headers.clientversion, | ||
apiversion: process.env.EQ_AUTHOR_API_VERSION, | ||
}, | ||
"Application version mismatch" | ||
); | ||
} | ||
next(); | ||
}; |
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 version = require("./version"); | ||
|
||
describe("version middleware", () => { | ||
let req; | ||
let res; | ||
let next; | ||
let previousEnv; | ||
|
||
beforeEach(() => { | ||
res = { | ||
json: jest.fn(), | ||
}; | ||
next = jest.fn(); | ||
previousEnv = process.env; | ||
process.env = { | ||
...process.env, | ||
EQ_AUTHOR_API_VERSION: "foo", | ||
}; | ||
}); | ||
|
||
it("should output a log on version mismatch", () => { | ||
req = { | ||
headers: { clientversion: "bar" }, | ||
log: { warn: jest.fn() }, | ||
}; | ||
|
||
version(req, res, next); | ||
|
||
expect(req.log.warn).toHaveBeenCalled(); | ||
|
||
expect(next).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should do nothing on version match", () => { | ||
req = { | ||
headers: { clientversion: "foo" }, | ||
log: { warn: jest.fn() }, | ||
}; | ||
|
||
version(req, res, next); | ||
|
||
expect(req.log.warn).not.toHaveBeenCalled(); | ||
|
||
expect(next).toHaveBeenCalled(); | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = previousEnv; | ||
}); | ||
}); |
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
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
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,8 @@ | ||
import appendAuthHeader from "./authHeader"; | ||
import appendVersionHeader from "./versionHeader"; | ||
import { flow } from "lodash"; | ||
|
||
export default flow( | ||
appendAuthHeader, | ||
appendVersionHeader | ||
); |
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,9 @@ | ||
export default headers => { | ||
const { REACT_APP_EQ_AUTHOR_VERSION } = process.env; | ||
return REACT_APP_EQ_AUTHOR_VERSION | ||
? { | ||
...headers, | ||
clientVersion: REACT_APP_EQ_AUTHOR_VERSION, | ||
} | ||
: headers; | ||
}; |
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,33 @@ | ||
import versionHeader from "./versionHeader"; | ||
|
||
describe("versionHeader", () => { | ||
let env; | ||
beforeEach(() => { | ||
env = Object.assign({}, process.env); | ||
}); | ||
afterEach(() => { | ||
process.env = env; | ||
}); | ||
it("should append a clientVersion field to an incoming object", () => { | ||
process.env.REACT_APP_EQ_AUTHOR_VERSION = "theBest"; | ||
|
||
const initialHeaders = { | ||
hello: "goodbye", | ||
}; | ||
const newHeaders = versionHeader(initialHeaders); | ||
|
||
expect(newHeaders).toMatchObject({ | ||
hello: "goodbye", | ||
clientVersion: "theBest", | ||
}); | ||
}); | ||
|
||
it("should not append a clientVersion field to the incoming object if author version not set", () => { | ||
const initialHeaders = { | ||
hello: "goodbye", | ||
}; | ||
const newHeaders = versionHeader(initialHeaders); | ||
|
||
expect(newHeaders.clientVersion).toBeUndefined(); | ||
}); | ||
}); |