Skip to content

Commit a52171c

Browse files
authored
test: Add network tests to bindings (#318)
1 parent a7514b4 commit a52171c

File tree

11 files changed

+225
-43
lines changed

11 files changed

+225
-43
lines changed

.github/workflows/build.yml

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ jobs:
7474
python -m pip install -r ./css-inline/tests/requirements-test.txt
7575
# Starts the server in background
7676
python ./css-inline/tests/server.py &
77+
shell: bash
7778

7879
- uses: dtolnay/rust-toolchain@stable
7980

@@ -251,6 +252,19 @@ jobs:
251252
cache: yarn
252253
cache-dependency-path: bindings/javascript/yarn.lock
253254

255+
- uses: actions/setup-python@v5
256+
with:
257+
python-version: 3.11
258+
cache: "pip"
259+
cache-dependency-path: "**/requirements-*.txt"
260+
261+
- name: Start background server
262+
run: |
263+
python -m pip install -r ./css-inline/tests/requirements-test.txt
264+
# Starts the server in background
265+
python ./css-inline/tests/server.py &
266+
shell: bash
267+
254268
- name: Install Rust
255269
uses: dtolnay/rust-toolchain@stable
256270
if: ${{ !matrix.settings.docker }}
@@ -343,18 +357,18 @@ jobs:
343357

344358
- name: Test (x86_64-unknown-linux-gnu)
345359
if: matrix.settings.target == 'x86_64-unknown-linux-gnu'
346-
run: docker run --rm -v $(pwd):/build -w /build/bindings/javascript node:${{ matrix.node }}-slim yarn test
360+
run: docker run --rm --network host -v $(pwd):/build -w /build/bindings/javascript node:${{ matrix.node }}-slim yarn test
347361

348362
- name: Test (x86_64-unknown-linux-musl)
349363
if: matrix.settings.target == 'x86_64-unknown-linux-musl'
350-
run: docker run --rm -v $(pwd):/build -w /build/bindings/javascript node:${{ matrix.node }}-alpine yarn test
364+
run: docker run --rm --network host -v $(pwd):/build -w /build/bindings/javascript node:${{ matrix.node }}-alpine yarn test
351365

352366
- name: Test (aarch64-unknown-linux-gnu)
353367
if: matrix.settings.target == 'aarch64-unknown-linux-gnu'
354368
uses: addnab/docker-run-action@v3
355369
with:
356370
image: node:${{ matrix.node }}-slim
357-
options: "--platform linux/arm64 -v ${{ github.workspace }}:/build -w /build/bindings/javascript"
371+
options: "--platform linux/arm64 --network host -v ${{ github.workspace }}:/build -w /build/bindings/javascript"
358372
run: |
359373
set -e
360374
yarn test
@@ -364,7 +378,7 @@ jobs:
364378
uses: addnab/docker-run-action@v3
365379
with:
366380
image: node:${{ matrix.node }}-alpine
367-
options: "--platform linux/arm64 -v ${{ github.workspace }}:/build -w /build/bindings/javascript"
381+
options: "--platform linux/arm64 --network host -v ${{ github.workspace }}:/build -w /build/bindings/javascript"
368382
run: |
369383
set -e
370384
yarn test
@@ -374,7 +388,7 @@ jobs:
374388
uses: addnab/docker-run-action@v3
375389
with:
376390
image: node:${{ matrix.node }}-bullseye-slim
377-
options: "--platform linux/arm/v7 -v ${{ github.workspace }}:/build -w /build/bindings/javascript"
391+
options: "--platform linux/arm/v7 --network host -v ${{ github.workspace }}:/build -w /build/bindings/javascript"
378392
run: |
379393
set -e
380394
yarn test
@@ -400,6 +414,13 @@ jobs:
400414
cache: "pip"
401415
cache-dependency-path: "**/requirements-*.txt"
402416

417+
- name: Start background server
418+
run: |
419+
python -m pip install -r ./css-inline/tests/requirements-test.txt
420+
# Starts the server in background
421+
python ./css-inline/tests/server.py &
422+
shell: bash
423+
403424
- run: python -m pip install -r requirements/dev.txt
404425
working-directory: ./bindings/python
405426

@@ -426,6 +447,20 @@ jobs:
426447
cargo-cache: true
427448
cache-version: v1
428449
working-directory: ./bindings/ruby
450+
451+
- uses: actions/setup-python@v5
452+
with:
453+
python-version: 3.11
454+
cache: "pip"
455+
cache-dependency-path: "**/requirements-*.txt"
456+
457+
- name: Start background server
458+
run: |
459+
python -m pip install -r ./css-inline/tests/requirements-test.txt
460+
# Starts the server in background
461+
python ./css-inline/tests/server.py &
462+
shell: bash
463+
429464
- run: bundle exec rake test
430465
working-directory: ./bindings/ruby
431466

@@ -530,6 +565,7 @@ jobs:
530565
python -m pip install -r ./css-inline/tests/requirements-test.txt
531566
# Starts the server in background
532567
python ./css-inline/tests/server.py &
568+
shell: bash
533569

534570
- name: Run tests
535571
run: cargo hack llvm-cov --no-report --feature-powerset

bindings/javascript/__test__/index.spec.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,74 @@ test("invalid style", (t) => {
6161
t.is(error.code, "GenericFailure");
6262
t.is(error.message, "Invalid @ rule: wrong");
6363
});
64+
65+
const inlinedHtml = `<html><head>
66+
67+
<link href="/rss.xml" rel="alternate" title="RSS" type="application/rss+xml">
68+
69+
</head>
70+
<body>
71+
<h1 style="color: blue;">Big Text</h1>
72+
<h2 style="color: red;">Smaller Text</h2>
73+
74+
</body></html>`;
75+
76+
test("remote file stylesheet", (t) => {
77+
t.is(
78+
inline(`<html>
79+
<head>
80+
<link href="../../css-inline/tests/external.css" rel="stylesheet">
81+
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml">
82+
<style>
83+
h2 { color: red; }
84+
</style>
85+
</head>
86+
<body>
87+
<h1>Big Text</h1>
88+
<h2>Smaller Text</h2>
89+
</body>
90+
</html>`),
91+
inlinedHtml,
92+
);
93+
});
94+
95+
test("remote network stylesheet", (t) => {
96+
t.is(
97+
inline(`<html>
98+
<head>
99+
<link href="http://127.0.0.1:1234/external.css" rel="stylesheet">
100+
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml">
101+
<style>
102+
h2 { color: red; }
103+
</style>
104+
</head>
105+
<body>
106+
<h1>Big Text</h1>
107+
<h2>Smaller Text</h2>
108+
</body>
109+
</html>`),
110+
inlinedHtml,
111+
);
112+
});
113+
114+
test("remote network relative stylesheet", (t) => {
115+
t.is(
116+
inline(
117+
`<html>
118+
<head>
119+
<link href="external.css" rel="stylesheet">
120+
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml">
121+
<style>
122+
h2 { color: red; }
123+
</style>
124+
</head>
125+
<body>
126+
<h1>Big Text</h1>
127+
<h2>Smaller Text</h2>
128+
</body>
129+
</html>`,
130+
{ baseUrl: "http://127.0.0.1:1234" },
131+
),
132+
inlinedHtml,
133+
);
134+
});

bindings/javascript/__test__/wasm.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ test("unsupported network operation", (t) => {
7979
const error = t.throws(
8080
() => {
8181
inline(
82-
"<html><head><link href='http://127.0.0.1:5000/external.css' rel='stylesheet' type='text/css'></head><body></body></html>",
82+
"<html><head><link href='http://127.0.0.1:1234/external.css' rel='stylesheet' type='text/css'></head><body></body></html>",
8383
);
8484
},
8585
{ any: true },
8686
);
8787
t.is(
8888
error,
89-
"Loading remote stylesheets is not supported on WASM: http://127.0.0.1:5000/external.css",
89+
"Loading remote stylesheets is not supported on WASM: http://127.0.0.1:1234/external.css",
9090
);
9191
});
9292

bindings/python/tests-py/test_inlining.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test_missing_stylesheet():
6666
css_inline.inline(
6767
"""<html>
6868
<head>
69-
<link href="tests/missing.css" rel="stylesheet" type="text/css">
69+
<link href="tests/missing.css" rel="stylesheet">
7070
</head>
7171
<body>
7272
<h1>Big Text</h1>
@@ -79,7 +79,7 @@ def test_file_scheme():
7979
css_inline.inline(
8080
"""<html>
8181
<head>
82-
<link href="external.css" rel="stylesheet" type="text/css">
82+
<link href="external.css" rel="stylesheet">
8383
</head>
8484
<body>
8585
<h1>Big Text</h1>
@@ -89,6 +89,46 @@ def test_file_scheme():
8989
)
9090

9191

92+
@pytest.mark.parametrize(
93+
"href, kwargs",
94+
(
95+
("http://127.0.0.1:1234/external.css", {}),
96+
("../../css-inline/tests/external.css", {}),
97+
("external.css", {"base_url": "http://127.0.0.1:1234"}),
98+
),
99+
)
100+
def test_remote_stylesheet(href, kwargs):
101+
inlined = css_inline.inline(
102+
f"""<html>
103+
<head>
104+
<link href="{href}" rel="stylesheet">
105+
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml">
106+
<style>
107+
h2 {{ color: red; }}
108+
</style>
109+
</head>
110+
<body>
111+
<h1>Big Text</h1>
112+
<h2>Smaller Text</h2>
113+
</body>
114+
</html>""",
115+
**kwargs,
116+
)
117+
assert (
118+
inlined
119+
== """<html><head>
120+
121+
<link href="/rss.xml" rel="alternate" title="RSS" type="application/rss+xml">
122+
123+
</head>
124+
<body>
125+
<h1 style="color: blue;">Big Text</h1>
126+
<h2 style="color: red;">Smaller Text</h2>
127+
128+
</body></html>"""
129+
)
130+
131+
92132
def test_invalid_base_url():
93133
with pytest.raises(ValueError, match="relative URL without a base: foo"):
94134
css_inline.CSSInliner(base_url="foo")
@@ -99,7 +139,7 @@ def test_invalid_href():
99139
css_inline.inline(
100140
"""<html>
101141
<head>
102-
<link href="http:" rel="stylesheet" type="text/css">
142+
<link href="http:" rel="stylesheet">
103143
</head>
104144
<body>
105145
</body>

bindings/ruby/spec/css_inline_spec.rb

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,39 @@ def make_html(style, body)
6464
expect(inlined[1]).to eq(expected)
6565
end
6666

67+
[
68+
["http://127.0.0.1:1234/external.css", {}],
69+
["../../css-inline/tests/external.css", {}],
70+
["external.css", {"base_url": "http://127.0.0.1:1234"}]
71+
].each do |href, kwargs|
72+
it "Resolves remote stylesheets: #{href}" do
73+
# Fails with the following on Windows:
74+
# error trying to connect: tcp set_nonblocking error: An operation was attempted on something that is not a socket. (os error 10038)
75+
skip 'Skipping on Windows' if RUBY_PLATFORM =~ /mswin|mingw|cygwin/
76+
inlined = CSSInline::inline("<html>
77+
<head>
78+
<link href=\"#{href}\" rel=\"stylesheet\">
79+
<style>
80+
h2 { color: red; }
81+
</style>
82+
</head>
83+
<body>
84+
<h1>Big Text</h1>
85+
<h2>Smaller Text</h2>
86+
</body>
87+
</html>", **kwargs)
88+
expect(inlined).to eq('''<html><head>
89+
90+
91+
</head>
92+
<body>
93+
<h1 style="color: blue;">Big Text</h1>
94+
<h2 style="color: red;">Smaller Text</h2>
95+
96+
</body></html>''')
97+
end
98+
end
99+
67100
it 'Shows the stylesheet location in base url errors' do
68101
expect { CSSInline::CSSInliner.new(base_url: 'foo') }.to raise_error('relative URL without a base: foo')
69102
end
@@ -73,7 +106,7 @@ def make_html(style, body)
73106
CSSInline::CSSInliner.new.inline('''
74107
<html>
75108
<head>
76-
<link href="http:" rel="stylesheet" type="text/css">
109+
<link href="http:" rel="stylesheet">
77110
</head>
78111
<body>
79112
</body>

css-inline/tests/example.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<html>
22
<head>
3-
<link href="external.css" rel="stylesheet" type="text/css">
3+
<link href="external.css" rel="stylesheet">
44
<style>
55
h1 {
66
text-decoration: none;

css-inline/tests/requirements-test.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
flask
1+
# Python 3.7 compatibility
2+
flask<2.3
3+
werkzeug<2.3

css-inline/tests/requirements-test.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
#
55
# pip-compile --resolver=backtracking requirements-test.in
66
#
7-
blinker==1.6.2
8-
# via flask
97
click==8.1.3
108
# via flask
11-
flask==2.3.2
9+
flask==2.2.5
1210
# via -r requirements-test.in
1311
itsdangerous==2.1.2
1412
# via flask
@@ -18,5 +16,7 @@ markupsafe==2.1.3
1816
# via
1917
# jinja2
2018
# werkzeug
21-
werkzeug==2.3.8
22-
# via flask
19+
werkzeug==2.2.3
20+
# via
21+
# -r requirements-test.in
22+
# flask

css-inline/tests/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ def stylesheet():
99

1010

1111
if __name__ == "__main__":
12-
app.run()
12+
app.run(host="0.0.0.0", port=1234)

css-inline/tests/test_cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub mod tests {
5959
fn wrong_base_url() {
6060
css_inline()
6161
.arg("--base-url=https://:::::")
62-
.write_stdin(r#"<html><head><link href="external.css" rel="stylesheet" type="text/css"></head><body><h1>Hello world!</h1></body></html>"#)
62+
.write_stdin(r#"<html><head><link href="external.css" rel="stylesheet"></head><body><h1>Hello world!</h1></body></html>"#)
6363
.assert()
6464
.failure()
6565
.stderr("Status: ERROR\nDetails: empty host\n");

0 commit comments

Comments
 (0)