Challenge #2b: Dynamic Configuration
In this challenge, you’ll modify the code you created in the previous challenge. Rather than hard-coding the ROT-13 service port and correlation ID, you’ll use values stored in WwwConfig
.
Instructions
1. Revise the "POST asks ROT-13 service to transform text"
test:
- Configure
WwwConfig
to use the port999
. - Configure
WwwConfig
to use the correlation ID"my-correlation-id"
. - Change the test’s assertion to use the new port and correlation ID.
2. Revise HomePageController.postAsync()
:
- Get the port and correlation ID out of the
config
parameter.
Remember to commit your changes when you’re done.
API Documentation
const config = WwwConfig.createTestInstance({ rot13ServicePort: 999, correlationId: "my-correlation-id" });
Create a WwwConfig
instance with the provided ROT-13 service port and correlation ID. Note that the parameter is an object—don’t forget the curly braces!
- rot13ServicePort
(number)
- the port of the ROT-13 service - correlationId
(string)
- a unique ID representing the user’s request - returns config
(WwwConfig)
- the configured instance
const port = config.rot13ServicePort;
Get the configured ROT-13 service port.
- returns port
(number)
- the port
const id = config.correlationId;
Get the configured correlation ID.
- returns id
(string)
- the ID
JavaScript Primers
No new concepts.
Hints
1
You need to configure the ROT-13 service port and correlation ID.
Change the call to WwwConfig.createTestInstance()
.
it("POST asks ROT-13 service to transform text", async () => {
// Arrange
const rot13Client = Rot13Client.createNull();
const rot13Requests = rot13Client.trackRequests();
const clock = Clock.createNull();
const controller = new HomePageController(rot13Client, clock);
const request = HttpServerRequest.createNull();
const config = WwwConfig.createTestInstance({
rot13ServicePort: 999,
correlationId: "my-correlation-id",
});
// Act
await controller.postAsync(request, config);
// Assert
assert.deepEqual(rot13Requests.data, [{
port: 123,
text: "some text",
correlationId: "0000-0000",
}]);
});
2
Don’t forget to update your assertion.
it("POST asks ROT-13 service to transform text", async () => {
// Arrange
const rot13Client = Rot13Client.createNull();
const rot13Requests = rot13Client.trackRequests();
const clock = Clock.createNull();
const controller = new HomePageController(rot13Client, clock);
const request = HttpServerRequest.createNull();
const config = WwwConfig.createTestInstance({
rot13ServicePort: 999,
correlationId: "my-correlation-id",
});
// Act
await controller.postAsync(request, config);
// Assert
assert.deepEqual(rot13Requests.data, [{
port: 999,
text: "some text",
correlationId: "my-correlation-id",
}]);
});
3
You’re ready to run the test.
It should fail, saying the port
and correlationId
are different than expected.
This means your production code isn’t using the values defined in the config.
4
Change your production code to get the port and correlation ID out of the config object.
Use config.rot13ServicePort
and config.correlationId
.
async postAsync(request, config) {
await this._rot13Client.transformAsync(
config.rot13ServicePort,
"some text",
config.correlationId,
);
}
TypeScript needs a placeholder return:
// TypeScript
async postAsync(request: HttpServerRequest, config: WwwConfig): Promise<HttpServerResponse> {
await this._rot13Client.transformAsync(
config.rot13ServicePort,
"some text",
config.correlationId,
);
// DELETE ME: placeholder to satisfy compiler
return await HttpServerResponse.createPlainTextResponse({ status: 501, body: "not implemented" });
}
Complete Solution
Test code:
it("POST asks ROT-13 service to transform text", async () => {
// Arrange
const rot13Client = Rot13Client.createNull();
const rot13Requests = rot13Client.trackRequests();
const clock = Clock.createNull();
const controller = new HomePageController(rot13Client, clock);
const request = HttpServerRequest.createNull();
const config = WwwConfig.createTestInstance({
rot13ServicePort: 999,
correlationId: "my-correlation-id",
});
// Act
await controller.postAsync(request, config);
// Assert
assert.deepEqual(rot13Requests.data, [{
port: 999,
text: "some text",
correlationId: "my-correlation-id",
}]);
});
Production code (JavaScript):
async postAsync(request, config) {
await this._rot13Client.transformAsync(
config.rot13ServicePort,
"some text",
config.correlationId,
);
}
Production code (TypeScript):
async postAsync(request: HttpServerRequest, config: WwwConfig): Promise<typescript> {
await this._rot13Client.transformAsync(
config.rot13ServicePort,
"some text",
config.correlationId,
);
// DELETE ME: placeholder to satisfy compiler
return await HttpServerResponse.createPlainTextResponse({ status: 501, body: "not implemented" });
}