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

Fetch implementation #619

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions graal-js/mx.graal-js/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
],
"requires" : [
"java.desktop",
"jdk.httpserver",
"jdk.unsupported",
],
"annotationProcessors" : ["truffle:TRUFFLE_DSL_PROCESSOR"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public enum JSErrorType {
SyntaxError,
TypeError,
URIError,
FetchError,
// since ES2021
AggregateError
}
168 changes: 168 additions & 0 deletions graal-js/src/com.oracle.truffle.js.test/js/fetch/headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
*/

/**
* Tests of the Headers class
*/

load('../assert.js');

(function shouldConformToIDL() {
const h = new Headers();
assertTrue(Reflect.has(h, 'append'));
assertTrue(Reflect.has(h, 'delete'));
assertTrue(Reflect.has(h, 'get'));
assertTrue(Reflect.has(h, 'has'));
assertTrue(Reflect.has(h, 'set'));
assertTrue(Reflect.has(h, 'forEach'));
})();

(function shouldProvideAppendMethod() {
const h = new Headers();
h.append('a', '1');
assertSame('1', h.get('a'));
})();

(function shouldProvideDeleteMethod() {
const h = new Headers({a: '1'});
h.delete('a');
assertFalse(h.has('a'));
})();

(function shouldProvideGetMethod() {
const h = new Headers({a: '1'});
assertSame('1', h.get('a'));
})();

(function shouldProvideSetMethod() {
const h = new Headers();
h.set('a', '1');
assertSame('1', h.get('a'));
})();

(function shouldSetOverwrite() {
const h = new Headers({a: '1'});
h.set('a', '2');
assertSame('2', h.get('a'));
})();

(function shouldProvideHasMethod() {
const h = new Headers({a: '1'});
assertTrue(h.has('a'));
assertFalse(h.has('foo'));
})();

(function shouldCreateListWhenAppending() {
const h = new Headers({'a': '1'});
h.append('a', '2');
h.append('a', '3');
assertSame('1, 2, 3', h.get('a'));
})();

(function shouldConvertHeaderNamesToLowercase() {
const h = new Headers({'A': '1', 'a': '2'});
h.append('A', '3');
h.append('a', '4');
h.set('Content-Type', 'application/json');

assertTrue(h.has('A') && h.has('a'));
assertSame('1, 2, 3, 4', h.get('a'));

assertSame('application/json', h.get('content-type'));
assertSame('application/json', h.get('Content-Type'));
})();

(function shouldAllowIteratingWithForEach() {
const headers = new Headers({'a': '1', 'b': '2', 'c': '3'});
const result = [];
headers.forEach((val, key, _) => {
result.push(`${key}: ${val}`);
});
assertSame("a: 1", result[0]);
assertSame("b: 2", result[1]);
assertSame("c: 3", result[2]);
})();

(function thisShouldBeUndefinedInForEach() {
const headers = new Headers();
headers.forEach(function() {
assertSame(undefined, this);
});
})();

(function shouldAcceptThisArgArgumentInForEach() {
const headers = new Headers();
const thisArg = {};
headers.forEach(function() {
assertSame(thisArg, this);
}, thisArg);
})();

(function shouldBeSortedByHeaderName() {
const h = new Headers({'c': '3', 'a' : '1', 'd': '4'});
h.append('b', '2');

const result = []
h.forEach((v, k) => {
result.push(`${k}: ${v}`);
})

assertSame('a: 1', result[0]);
assertSame('b: 2', result[1]);
assertSame('c: 3', result[2]);
assertSame('d: 4', result[3]);
})();

(function shouldValidateHeaders() {
// invalid header
assertThrows(() => new Headers({'': 'ok'}), TypeError);
assertThrows(() => new Headers({'HE y': 'ok'}), TypeError);
assertThrows(() => new Headers({'Hé-y': 'ok'}), TypeError);
// invalid value
assertThrows(() => new Headers({'HE-y': 'ăk'}), TypeError);
})();

(function shouldValidateHeadersInMethods() {
const headers = new Headers();
assertThrows(() => headers.append('', 'ok'), TypeError);
assertThrows(() => headers.append('Hé-y', 'ok'), TypeError);
assertThrows(() => headers.append('HE-y', 'ăk'), TypeError);
assertThrows(() => headers.delete('Hé-y'), TypeError);
assertThrows(() => headers.get('Hé-y'), TypeError);
assertThrows(() => headers.has('Hé-y'), TypeError);
assertThrows(() => headers.set('Hé-y', 'ok'), TypeError);
assertThrows(() => headers.set('HE-y', 'ăk'), TypeError);
})();

(function shouldNormalizeValues() {
const headers = new Headers({'a': ' 1', });
headers.append('b', '2 ');
headers.set('c', ' 3 ');
assertSame('1', headers.get('a'));
assertSame('2', headers.get('b'));
assertSame('3', headers.get('c'));
})();

(function shouldWrapHeadersObject() {
const h1 = new Headers({'a': '1'});

const h2 = new Headers(h1);
h2.set('b', '1');

const h3 = new Headers(h2);
h3.append('a', '2');

assertFalse(h1.has('b'));
assertTrue(h2.has('a'));
assertSame('1, 2', h3.get('a'));
})();

(function shouldRejectIncorrectConstructorArguments() {
assertThrows(() => new Headers(''), TypeError);
assertThrows(() => new Headers(0), TypeError);
assertThrows(() => new Headers(false), TypeError);
})();
26 changes: 26 additions & 0 deletions graal-js/src/com.oracle.truffle.js.test/js/fetch/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
*/

/**
* Test non-networking behaviour of the fetch api
*/

load('../assert.js');

const url = "http://localhost:8080";

(function shouldExposeHeaderRequestResponse() {
assertTrue(new Headers() instanceof Headers);
assertTrue(new Request(url) instanceof Request);
assertTrue(new Response() instanceof Response);
})();

(function shouldSupportProperStringOutput () {
assertSame('[object Headers]', new Headers().toString());
assertSame('[object Request]', new Request(url).toString());
assertSame('[object Response]', new Response().toString());
})();
Loading