Skip to content

Commit

Permalink
Merge pull request #325 from drashland/issue-#324-execute-middleware-…
Browse files Browse the repository at this point in the history
…for-static-assets

Issue #324 execute middleware for static assets
  • Loading branch information
crookse authored Jul 18, 2020
2 parents 19b1c98 + af69f87 commit cd8de7c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
7 changes: 2 additions & 5 deletions src/http/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,11 @@ export class Response {
*
* @returns The final output to be sent.
*/
public sendStatic(
file: null | string,
contents: Uint8Array | string = "",
): Drash.Interfaces.ResponseOutput {
public sendStatic(): Drash.Interfaces.ResponseOutput {
let output: Drash.Interfaces.ResponseOutput = {
status: this.status_code,
headers: this.headers,
body: file ? Deno.readFileSync(file) : contents,
body: this.body as Uint8Array,
};

this.request.respond(output);
Expand Down
27 changes: 18 additions & 9 deletions src/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,33 +409,42 @@ export class Server {
request: Drash.Http.Request,
): Promise<Drash.Interfaces.ResponseOutput> {
try {
await this.executeMiddlewareServerLevelBeforeRequest(request);

const response = new Drash.Http.Response(request, {
views_path: this.configs.views_path,
template_engine: this.configs.template_engine,
});

if (this.configs.pretty_links == null) {
return response.sendStatic(`${this.directory}/${request.url}`);
}

const extension = request.url.split(".")[1];
if (extension != null) {
return response.sendStatic(`${this.directory}/${request.url}`);

if (
this.configs.pretty_links == null ||
extension != null
) {
response.body = Deno.readFileSync(`${this.directory}/${request.url}`);
await this.executeMiddlewareServerLevelAfterRequest(request, response);
return response.sendStatic();
}

const contents = Deno.readFileSync(
`${this.directory}/${request.url}/index.html`,
);

if (contents == null) {
return response.sendStatic(`${this.directory}/${request.url}`);
response.body = Deno.readFileSync(`${this.directory}/${request.url}`);
await this.executeMiddlewareServerLevelAfterRequest(request, response);
return response.sendStatic();
}

response.headers.set("Content-Type", "text/html");
return response.sendStatic(null, contents);
response.body = contents;
await this.executeMiddlewareServerLevelAfterRequest(request, response);
return response.sendStatic();
} catch (error) {
return await this.handleHttpRequestError(
request,
this.httpErrorResponse(404),
this.httpErrorResponse(error.code ?? 404, error.message),
);
}
}
Expand Down
46 changes: 46 additions & 0 deletions tests/unit/http/middleware_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,36 @@ Rhum.testPlan("http/middleware_test.ts", () => {
const response = await server.handleHttpRequest(request);
members.assertResponseJsonEquals(members.responseBody(response), "got");
});

Rhum.testCase("before_request: static path asset", async () => {
let server = new Drash.Http.Server({
static_paths: ["/assets"],
middleware: {
before_request: [BeforeRequestStaticPathAsset],
},
});
const request = members.mockRequest("/assets/test.js", "get");
const response = await server.handleHttpRequest(request);
members.assertResponseJsonEquals(
members.responseBody(response),
"Hello, I'm a static path asset before request middleware.",
);
});

Rhum.testCase("after_request: static path asset", async () => {
let server = new Drash.Http.Server({
static_paths: ["/assets"],
middleware: {
after_request: [AfterRequestStaticPathAsset],
},
});
const request = members.mockRequest("/assets/test.js", "get");
const response = await server.handleHttpRequest(request);
members.assertResponseJsonEquals(
members.responseBody(response),
"this static path asset's contents got changed",
);
});
});
});

Expand Down Expand Up @@ -218,3 +248,19 @@ function VerifyCsrfToken(req: Drash.Http.Request) {
);
}
}

function BeforeRequestStaticPathAsset(req: Drash.Http.Request) {
throw new Drash.Exceptions.HttpException(
418,
"Hello, I'm a static path asset before request middleware.",
);
}

function AfterRequestStaticPathAsset(
req: Drash.Http.Request,
res?: Drash.Http.Response,
) {
if (res) {
res.body = "this static path asset's contents got changed";
}
}
3 changes: 2 additions & 1 deletion tests/unit/http/response_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ Rhum.testPlan("http/response_test.ts", () => {
Rhum.testCase("Returns the contents if a file is passed in", async () => {
const request = members.mockRequest("/");
const response = new Drash.Http.Response(request);
const actual = response.sendStatic("./tests/data/static_file.txt");
response.body = Deno.readFileSync("./tests/data/static_file.txt");
const actual = response.sendStatic();
const headers = new Headers();
headers.set("content-type", "undefined");
const expected = {
Expand Down

0 comments on commit cd8de7c

Please sign in to comment.