koajs / koajs/workshop

06-content-negotiation, "content-type" required when encoding is "gzip"

Open
#20 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
442
Forks
104
PR merge metrics
No merged PRs in 30d

Description

When accept-encoding is gzip, we need to set the content-type to text/plain.

this.response.set('Content-Encoding', 'gzip');
this.response.body = yield gzip('hello world');

The above code would fail the test because, when content-enconding is gzip, the default content-type is application/octet-stream.

Adding the content-type assignment would solve the problem.

this.response.set('Content-Encoding', 'gzip');
this.response.set('content-type', 'text/plain');
this.response.body = yield gzip('hello world');
It takes me a lot time to understand what this chapter is focusing on.

Here is one solution in case anyone need.

app.use(function* () {
  const ae = this.request.acceptsEncodings('gzip', 'identity');

  switch (ae) {
    case 'gzip':
      this.response.set('Content-Encoding', 'gzip');
      this.response.set('content-type', 'text/plain');
      this.response.body = yield gzip('hello world');
      break;
    case 'identity':
      this.response.set('Content-Encoding', 'identity');
      this.response.body = 'hello world';
      break;
    default:
      break;
  }
});

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the chapter 06 content-negotiation example and the test that fails for a gzip response. Verify the response handling shown in the issue, then ensure the gzip case declares text/plain as its content type and the test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.