Commit | Line | Data |
---|---|---|
1c93f4de MG |
1 | #!/usr/bin/perl |
2 | use 5.014000; | |
3 | use warnings; | |
4 | ||
5 | use Test::More tests => 14; | |
6 | BEGIN { use_ok('Plack::Middleware::BasicStyle') }; | |
7 | ||
8 | use HTTP::Request::Common; | |
9 | use Plack::Builder; | |
10 | use Plack::Test; | |
11 | ||
12 | my $default_hdrs = ['Content-Type' => 'text/html; charset=utf-8']; | |
13 | ||
14 | sub run_test { | |
15 | my ($args, $hdrs, $body, $expected, $title, $url) = @_; | |
16 | $url //= '/'; | |
17 | test_psgi | |
18 | builder { | |
19 | enable 'BasicStyle', @$args; | |
20 | sub { [200, $hdrs, [$body]] } | |
21 | }, | |
22 | sub { | |
23 | my ($cb) = @_; | |
24 | my $result = $cb->(GET $url); | |
25 | if (ref $expected eq 'ARRAY') { | |
26 | my ($hdr, $exp) = @$expected; | |
27 | is $result->header($hdr), $exp, $title | |
28 | } else { | |
29 | is $result->content, $expected, $title | |
30 | } | |
31 | } | |
32 | } | |
33 | ||
34 | run_test [], $default_hdrs, <<'BODY', <<'EXPECTED', 'default'; | |
35 | <!DOCTYPE html> | |
36 | <html> | |
37 | <head> | |
38 | <meta charset="utf-8"> | |
39 | <title>Foo</title> | |
40 | </head> | |
41 | <body> | |
42 | <h1>Bar</h1> | |
43 | </body> | |
44 | </html> | |
45 | BODY | |
46 | <!DOCTYPE html> | |
47 | <html> | |
48 | <head><style>body{margin:40pxauto;max-width:650px;line-height:1.6;font-size:18px;color:#444;padding:010px}h1,h2,h3{line-height:1.2}</style> | |
49 | <meta charset="utf-8"> | |
50 | <title>Foo</title> | |
51 | </head> | |
52 | <body> | |
53 | <h1>Bar</h1> | |
54 | </body> | |
55 | </html> | |
56 | EXPECTED | |
57 | ||
58 | local $Plack::Middleware::BasicStyle::DEFAULT_STYLE = '<here>'; | |
59 | ||
60 | run_test [], $default_hdrs, <<'BODY', <<'EXPECTED', 'no head'; | |
61 | <html> | |
62 | content | |
63 | BODY | |
64 | <html><here> | |
65 | content | |
66 | EXPECTED | |
67 | ||
68 | run_test [], $default_hdrs, <<'BODY', <<'EXPECTED', 'no html'; | |
69 | <head> | |
70 | content | |
71 | BODY | |
72 | <head><here> | |
73 | content | |
74 | EXPECTED | |
75 | ||
76 | run_test [], $default_hdrs, 'content', '<here>content', 'no head, no html'; | |
77 | ||
78 | run_test [], $default_hdrs, '<!DOCTYPE html>', '<!DOCTYPE html><here>', 'just doctype'; | |
79 | ||
80 | run_test [], [], 'no change', 'no change', 'no content-type'; | |
81 | ||
82 | run_test [any_content_type => 1], [], 'yes change', '<here>yes change', 'no content-type + any_content_type'; | |
83 | ||
84 | run_test [], $default_hdrs, (<<'BODY') x 2, 'has <style>'; | |
85 | <!DOCTYPE html> | |
86 | <head> | |
87 | <style>h1 { color: red; }</style> | |
88 | content | |
89 | BODY | |
90 | ||
91 | run_test [], $default_hdrs, (<<'BODY') x 2, 'has external stylesheet'; | |
92 | <!DOCTYPE html> | |
93 | <html>> | |
94 | <link href="/style.css" rel="stylesheet"> | |
95 | content | |
96 | BODY | |
97 | ||
98 | run_test [even_if_styled => 1], $default_hdrs, | |
99 | <<'BODY', <<'EXPECTED', 'has <style> + even_if_styled'; | |
100 | <!DOCTYPE html> | |
101 | <style>h1 { color: red; }</style> | |
102 | content | |
103 | BODY | |
104 | <!DOCTYPE html><here> | |
105 | <style>h1 { color: red; }</style> | |
106 | content | |
107 | EXPECTED | |
108 | ||
109 | run_test [style => '<there>'], $default_hdrs, 'content', '<there>content', 'style'; | |
110 | ||
111 | run_test [use_link_header => '/basic-style.css'], | |
112 | $default_hdrs, 'test', ['Link', '</basic-style.css>; rel=stylesheet'], 'use_link_header'; | |
113 | ||
114 | run_test [use_link_header => '/basic-style.css'], | |
115 | $default_hdrs, 'test', '<here>', 'use_link_header - /basic-style.css', '/basic-style.css'; |