Skip to content

Commit

Permalink
Refactor fetchEndpointAsText to return res alongside data
Browse files Browse the repository at this point in the history
Split out of #121
where we use `res.url`.
  • Loading branch information
MadLittleMods committed Nov 3, 2022
1 parent 91d84fc commit cbef407
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion server/lib/fetch-endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function fetchEndpoint(endpoint, options = {}) {
async function fetchEndpointAsText(endpoint, options) {
const res = await fetchEndpoint(endpoint, options);
const data = await res.text();
return data;
return { data, res };
}

async function fetchEndpointAsJson(endpoint, options) {
Expand Down
36 changes: 19 additions & 17 deletions test/e2e-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('matrix-public-archive', () => {
assert.strictEqual(eventIds.length, 3);

archiveUrl = matrixPublicArchiveURLCreator.archiveUrlForDate(roomId, archiveDate);
const archivePageHtml = await fetchEndpointAsText(archiveUrl);
const { data: archivePageHtml } = await fetchEndpointAsText(archiveUrl);

const dom = parseHTML(archivePageHtml);

Expand Down Expand Up @@ -358,7 +358,7 @@ describe('matrix-public-archive', () => {

archiveUrl = matrixPublicArchiveURLCreator.archiveUrlForDate(roomId, archiveDate);

const archivePageHtml = await fetchEndpointAsText(archiveUrl);
const { data: archivePageHtml } = await fetchEndpointAsText(archiveUrl);

const dom = parseHTML(archivePageHtml);

Expand Down Expand Up @@ -445,7 +445,7 @@ describe('matrix-public-archive', () => {
viaServers: [HOMESERVER_URL_TO_PRETTY_NAME_MAP[testMatrixServerUrl2]],
});

const archivePageHtml = await fetchEndpointAsText(archiveUrl);
const { data: archivePageHtml } = await fetchEndpointAsText(archiveUrl);

const dom = parseHTML(archivePageHtml);

Expand Down Expand Up @@ -477,7 +477,7 @@ describe('matrix-public-archive', () => {

// Visit `/:roomIdOrAlias` and expect to be redirected to the last day with events
archiveUrl = matrixPublicArchiveURLCreator.archiveUrlForRoom(roomId);
const archivePageHtml = await fetchEndpointAsText(archiveUrl);
const { data: archivePageHtml } = await fetchEndpointAsText(archiveUrl);

const dom = parseHTML(archivePageHtml);

Expand Down Expand Up @@ -516,7 +516,7 @@ describe('matrix-public-archive', () => {
'The date we visit the archive (`visitArchiveDate`) should be after where the messages were sent (`archiveDate`)'
);
archiveUrl = matrixPublicArchiveURLCreator.archiveUrlForDate(roomId, visitArchiveDate);
const archivePageHtml = await fetchEndpointAsText(archiveUrl);
const { data: archivePageHtml } = await fetchEndpointAsText(archiveUrl);

const dom = parseHTML(archivePageHtml);

Expand Down Expand Up @@ -545,7 +545,7 @@ describe('matrix-public-archive', () => {
// We purposely send no events in the room

archiveUrl = matrixPublicArchiveURLCreator.archiveUrlForDate(roomId, archiveDate);
const archivePageHtml = await fetchEndpointAsText(archiveUrl);
const { data: archivePageHtml } = await fetchEndpointAsText(archiveUrl);

const dom = parseHTML(archivePageHtml);

Expand Down Expand Up @@ -574,7 +574,7 @@ describe('matrix-public-archive', () => {
});

archiveUrl = matrixPublicArchiveURLCreator.archiveUrlForDate(roomId, archiveDate);
const archivePageHtml = await fetchEndpointAsText(archiveUrl);
const { data: archivePageHtml } = await fetchEndpointAsText(archiveUrl);

assert.match(archivePageHtml, /TODO: Redirect user to smaller hour range/);
});
Expand Down Expand Up @@ -609,7 +609,7 @@ describe('matrix-public-archive', () => {
});

archiveUrl = matrixPublicArchiveURLCreator.archiveUrlForDate(roomId, archiveDate);
const archivePageHtml = await fetchEndpointAsText(archiveUrl);
const { data: archivePageHtml } = await fetchEndpointAsText(archiveUrl);

const dom = parseHTML(archivePageHtml);

Expand Down Expand Up @@ -718,7 +718,7 @@ describe('matrix-public-archive', () => {
);
// Set this for debugging if the test fails here
archiveUrl = firstPageArchiveUrl;
const firstPageArchivePageHtml = await fetchEndpointAsText(firstPageArchiveUrl);
const { data: firstPageArchivePageHtml } = await fetchEndpointAsText(firstPageArchiveUrl);
const firstPageDom = parseHTML(firstPageArchivePageHtml);

const eventIdsOnFirstPage = [...firstPageDom.document.querySelectorAll(`[data-event-id]`)]
Expand Down Expand Up @@ -746,7 +746,7 @@ describe('matrix-public-archive', () => {
const nextActivityLink = nextActivityLinkEl.getAttribute('href');
// Set this for debugging if the test fails here
archiveUrl = nextActivityLink;
const nextActivityArchivePageHtml = await fetchEndpointAsText(nextActivityLink);
const { data: nextActivityArchivePageHtml } = await fetchEndpointAsText(nextActivityLink);
const nextActivityDom = parseHTML(nextActivityArchivePageHtml);

// Assert that it's a smooth continuation to more messages with no overlap
Expand Down Expand Up @@ -792,7 +792,7 @@ describe('matrix-public-archive', () => {
);
// Set this for debugging if the test fails here
archiveUrl = firstPageArchiveUrl;
const firstPageArchivePageHtml = await fetchEndpointAsText(firstPageArchiveUrl);
const { data: firstPageArchivePageHtml } = await fetchEndpointAsText(firstPageArchiveUrl);
const firstPageDom = parseHTML(firstPageArchivePageHtml);

const eventIdsOnFirstPage = [...firstPageDom.document.querySelectorAll(`[data-event-id]`)]
Expand Down Expand Up @@ -820,7 +820,9 @@ describe('matrix-public-archive', () => {
const previousActivityLink = previousActivityLinkEl.getAttribute('href');
// Set this for debugging if the test fails here
archiveUrl = previousActivityLink;
const previousActivityArchivePageHtml = await fetchEndpointAsText(previousActivityLink);
const { data: previousActivityArchivePageHtml } = await fetchEndpointAsText(
previousActivityLink
);
const previousActivityDom = parseHTML(previousActivityArchivePageHtml);

// Assert that it's a smooth continuation to more messages with no overlap
Expand Down Expand Up @@ -868,7 +870,7 @@ describe('matrix-public-archive', () => {

// Browse the room directory without search to see many rooms
archiveUrl = matrixPublicArchiveURLCreator.roomDirectoryUrl();
const roomDirectoryPageHtml = await fetchEndpointAsText(archiveUrl);
const { data: roomDirectoryPageHtml } = await fetchEndpointAsText(archiveUrl);
const dom = parseHTML(roomDirectoryPageHtml);

const roomsOnPageWithoutSearch = [
Expand All @@ -882,7 +884,7 @@ describe('matrix-public-archive', () => {
archiveUrl = matrixPublicArchiveURLCreator.roomDirectoryUrl({
searchTerm: roomPlanetPrefix,
});
const roomDirectoryWithSearchPageHtml = await fetchEndpointAsText(archiveUrl);
const { data: roomDirectoryWithSearchPageHtml } = await fetchEndpointAsText(archiveUrl);
const domWithSearch = parseHTML(roomDirectoryWithSearchPageHtml);

const roomsOnPageWithSearch = [
Expand Down Expand Up @@ -919,7 +921,7 @@ describe('matrix-public-archive', () => {
homeserver: HOMESERVER_URL_TO_PRETTY_NAME_MAP[testMatrixServerUrl2],
searchTerm: roomPlanetPrefix,
});
const roomDirectoryWithSearchPageHtml = await fetchEndpointAsText(archiveUrl);
const { data: roomDirectoryWithSearchPageHtml } = await fetchEndpointAsText(archiveUrl);
const domWithSearch = parseHTML(roomDirectoryWithSearchPageHtml);

// Make sure the `?homserver` is selected in the homeserver selector `<select>`
Expand Down Expand Up @@ -975,7 +977,7 @@ describe('matrix-public-archive', () => {
const roomId = await createTestRoom(client);

archiveUrl = matrixPublicArchiveURLCreator.archiveUrlForRoom(roomId);
const archivePageHtml = await fetchEndpointAsText(archiveUrl);
const { data: archivePageHtml } = await fetchEndpointAsText(archiveUrl);

const dom = parseHTML(archivePageHtml);

Expand All @@ -994,7 +996,7 @@ describe('matrix-public-archive', () => {
});

archiveUrl = matrixPublicArchiveURLCreator.archiveUrlForRoom(roomId);
const archivePageHtml = await fetchEndpointAsText(archiveUrl);
const { data: archivePageHtml } = await fetchEndpointAsText(archiveUrl);

const dom = parseHTML(archivePageHtml);

Expand Down

0 comments on commit cbef407

Please sign in to comment.