Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve fetch #853

Merged
merged 5 commits into from
Sep 30, 2018
Merged

Improve fetch #853

merged 5 commits into from
Sep 30, 2018

Conversation

ztplz
Copy link
Contributor

@ztplz ztplz commented Sep 28, 2018

#522 Base on spec-whatwg and more test

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you - the tests need some clean up - I left some comments there.

@qti3e - please review if you have time and desire.

js/fetch_test.ts Outdated
const h = new Headers();
} catch (e) {
assert(false, "Create headers from no parameter failed");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need try catch blocks for successful tests. The test() function already does that.

You do need to add a name to the function, because that is used in the test output. So, for example, this test should be something like:

test(function newHeadersSuccess() {
    new Headers();
});

js/fetch_test.ts Outdated
} catch (e) {
assert(false, "Create headers from undefined parameter failed");
}
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

js/fetch_test.ts Outdated
} catch (e) {
assert(false, "Create headers from empty object failed");
}
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

} catch (e) {
assertEqual(e.message, "Failed to construct 'Headers': Invalid value");
}
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just combine all of these into one test case....

js/fetch_test.ts Outdated
});
headers.forEach(function(value, key, container) {
assertEqual(headers, container);
assertEqual(value, headerEntriesDict[key]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should run ./tools/format.py too. Use arrow notation for anonymous functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ry Done.

js/fetch_test.ts Outdated
@@ -93,30 +72,30 @@ for (const name in headerDict) {
headerSeq.push([name, headerDict[name]]);
}

test(function() {
test(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs name

js/fetch_test.ts Outdated
const headers = new Headers(headerSeq);
for (const name in headerDict) {
assertEqual(headers.get(name), String(headerDict[name]));
}
assertEqual(headers.get("length"), null);
});

test(function() {
test(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs name

value: init[key]
});
}
// init is a object
Copy link
Contributor

@qti3e qti3e Sep 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but it's not correct, your logic fails when init is some number.
try:

new DenoHeader(5);

I suggest is to do this:

if (arguments.length === 0 || init === undefined) {
  return;
}
if (init instanceof DenoHeaders) {
  ...
} else if (Array.isArray(init)) {
  ...
} else if (Object.prototype.toString.call(init) === "[object Object]") {
  // You can use a better is-object test if you have any : )
  ...
} else {
  throw new TypeError("Failed to construct 'Headers': Invalid value");
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why type check doesn't work. Maybe it is better to add more check in contructor fucntion.

}
}

private normalizeParams(name: string, value?: string): string[] {
name = String(name).toLowerCase();
value = String(value).trim();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trim removes all HTTP whitespace characters? I don't know about this...
But anyway it's just fine for now

});
headers.forEach((value, key, container) => {
assertEqual(headers, container);
assertEqual(value, headerEntriesDict[key]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if forEach does not call the callback at all?

let call_num = 0;
headers.forEach((value, key, container) => {
  ++call_num;
  ..
});
assertEqual(call_num, keys.length)

Copy link
Contributor

@qti3e qti3e left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, just a few comments

@ztplz
Copy link
Contributor Author

ztplz commented Sep 28, 2018

@qti3e Thanks.

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thanks @ztplz !

@ry ry merged commit b635553 into denoland:master Sep 30, 2018
@ztplz ztplz deleted the improve_fetch branch September 30, 2018 19:25
ry added a commit to ry/deno that referenced this pull request Oct 4, 2018
- Improve fetch headers (denoland#853)
- Add deno.truncate (denoland#805)
- Add copyFile/copyFileSync (denoland#863)
- Limit depth of output in console.log for nested objects, and add
  console.dir (denoland#826)
- Guess extensions on extension not provided (denoland#859)
- Renames:
  deno.platform -> deno.platform.os
  deno.arch -> deno.platform.arch
- Upgrade TS to 3.0.3
- Add readDirSync(), readDir()
- Add support for TCP servers and clients. (denoland#884)
  Adds deno.listen(), deno.dial(), deno.Listener and deno.Conn.
@ry ry mentioned this pull request Oct 4, 2018
ry added a commit that referenced this pull request Oct 4, 2018
- Improve fetch headers (#853)
- Add deno.truncate (#805)
- Add copyFile/copyFileSync (#863)
- Limit depth of output in console.log for nested objects, and add
  console.dir (#826)
- Guess extensions on extension not provided (#859)
- Renames:
  deno.platform -> deno.platform.os
  deno.arch -> deno.platform.arch
- Upgrade TS to 3.0.3
- Add readDirSync(), readDir()
- Add support for TCP servers and clients. (#884)
  Adds deno.listen(), deno.dial(), deno.Listener and deno.Conn.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants