Initial commit
[plack-middleware-basicstyle.git] / t / Plack-Middleware-BasicStyle.t
CommitLineData
1c93f4de
MG
1#!/usr/bin/perl
2use 5.014000;
3use warnings;
4
5use Test::More tests => 14;
6BEGIN { use_ok('Plack::Middleware::BasicStyle') };
7
8use HTTP::Request::Common;
9use Plack::Builder;
10use Plack::Test;
11
12my $default_hdrs = ['Content-Type' => 'text/html; charset=utf-8'];
13
14sub 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
34run_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>
45BODY
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>
56EXPECTED
57
58local $Plack::Middleware::BasicStyle::DEFAULT_STYLE = '<here>';
59
60run_test [], $default_hdrs, <<'BODY', <<'EXPECTED', 'no head';
61<html>
62content
63BODY
64<html><here>
65content
66EXPECTED
67
68run_test [], $default_hdrs, <<'BODY', <<'EXPECTED', 'no html';
69<head>
70content
71BODY
72<head><here>
73content
74EXPECTED
75
76run_test [], $default_hdrs, 'content', '<here>content', 'no head, no html';
77
78run_test [], $default_hdrs, '<!DOCTYPE html>', '<!DOCTYPE html><here>', 'just doctype';
79
80run_test [], [], 'no change', 'no change', 'no content-type';
81
82run_test [any_content_type => 1], [], 'yes change', '<here>yes change', 'no content-type + any_content_type';
83
84run_test [], $default_hdrs, (<<'BODY') x 2, 'has <style>';
85<!DOCTYPE html>
86<head>
87<style>h1 { color: red; }</style>
88content
89BODY
90
91run_test [], $default_hdrs, (<<'BODY') x 2, 'has external stylesheet';
92<!DOCTYPE html>
93<html>>
94<link href="/style.css" rel="stylesheet">
95content
96BODY
97
98run_test [even_if_styled => 1], $default_hdrs,
99 <<'BODY', <<'EXPECTED', 'has <style> + even_if_styled';
100<!DOCTYPE html>
101<style>h1 { color: red; }</style>
102content
103BODY
104<!DOCTYPE html><here>
105<style>h1 { color: red; }</style>
106content
107EXPECTED
108
109run_test [style => '<there>'], $default_hdrs, 'content', '<there>content', 'style';
110
111run_test [use_link_header => '/basic-style.css'],
112 $default_hdrs, 'test', ['Link', '</basic-style.css>; rel=stylesheet'], 'use_link_header';
113
114run_test [use_link_header => '/basic-style.css'],
115 $default_hdrs, 'test', '<here>', 'use_link_header - /basic-style.css', '/basic-style.css';
This page took 0.015389 seconds and 4 git commands to generate.