rack-bunto 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/helper.rb ADDED
@@ -0,0 +1,72 @@
1
+ require "minitest/autorun"
2
+ require "stringio"
3
+ require "time"
4
+ require "rack/mock"
5
+
6
+ require_relative "../lib/rack/bunto"
7
+
8
+ TEST_DIR = File.expand_path("..", __FILE__)
9
+
10
+
11
+ def silence_warnings
12
+ original_verbose, $VERBOSE = $VERBOSE, nil
13
+
14
+ yield
15
+ ensure
16
+ $VERBOSE = original_verbose
17
+ end
18
+
19
+ def silence_output
20
+ original_stderr, original_stdout = $stderr, $stdout
21
+ $stderr, $stdout = StringIO.new, StringIO.new
22
+
23
+ yield
24
+ ensure
25
+ $stderr, $stdout = original_stderr, original_stdout
26
+ end
27
+
28
+ def without_processing
29
+ silence_warnings do
30
+ Bunto::Site.class_eval do
31
+ alias :original_process :process
32
+ def process; end
33
+ end
34
+ end
35
+
36
+ yield
37
+ ensure
38
+ silence_warnings do
39
+ Bunto::Site.class_eval do
40
+ alias :process :original_process
41
+ remove_method :original_process
42
+ end
43
+ end
44
+ end
45
+
46
+ def rack_bunto(options = {})
47
+ bunto = nil
48
+ silence_output do
49
+ bunto = Rack::Bunto.new(options)
50
+ end
51
+
52
+ bunto
53
+ end
54
+
55
+ def rack_bunto_without_build(options = {})
56
+ bunto = nil
57
+ without_processing do
58
+ bunto = rack_bunto(options)
59
+ end
60
+
61
+ bunto
62
+ end
63
+
64
+ def file_must_exist(filename)
65
+ assert File.exist?(filename),
66
+ "Expected file `#{filename}' to exist."
67
+ end
68
+
69
+ def file_wont_exist(filename)
70
+ assert !File.exist?(filename),
71
+ "Expected file `#{filename}' to not exist."
72
+ end
@@ -0,0 +1 @@
1
+ fake_option: "ok"
@@ -0,0 +1,3 @@
1
+ ---
2
+ ---
3
+ Rack-Bunto post
@@ -0,0 +1,3 @@
1
+ ---
2
+ ---
3
+ ¡Buenos días!
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,3 @@
1
+ ---
2
+ ---
3
+ Test
@@ -0,0 +1,3 @@
1
+ ---
2
+ ---
3
+ Test
@@ -0,0 +1,3 @@
1
+ ---
2
+ ---
3
+ Rack-Bunto Test
@@ -0,0 +1 @@
1
+
@@ -0,0 +1 @@
1
+
@@ -0,0 +1 @@
1
+ Test
@@ -0,0 +1,55 @@
1
+ require_relative "helper"
2
+
3
+
4
+ describe "when initializing a Rack::Bunto instance" do
5
+
6
+ before do
7
+ @tempdir = File.join(TEST_DIR, "tmp")
8
+ FileUtils.mkdir_p(@tempdir) unless File.exist?(@tempdir)
9
+ Dir.chdir(@tempdir)
10
+
11
+ @sourcedir = File.join(TEST_DIR, "source")
12
+ @destdir = File.join(@tempdir, "_site")
13
+ FileUtils.mkdir_p(@sourcedir) unless File.exist?(@sourcedir)
14
+ FileUtils.mkdir_p(@destdir) unless File.exist?(@destdir)
15
+
16
+ @page = File.join(@destdir, "index.html")
17
+ @fake_page = File.join(@destdir, "fake.html")
18
+ end
19
+
20
+ after do
21
+ FileUtils.rm_rf(@tempdir) if File.exist?(@tempdir)
22
+ end
23
+
24
+ describe "when non-empty destination directory exists" do
25
+
26
+ before do
27
+ FileUtils.touch(@fake_page)
28
+ end
29
+
30
+ it "should not build the site by default" do
31
+ file_wont_exist(@page)
32
+ rack_bunto(:source => @sourcedir,
33
+ :destination => @destdir)
34
+ file_wont_exist(@page)
35
+ end
36
+
37
+ it "should build the site when :force_build option is set" do
38
+ file_wont_exist(@page)
39
+ rack_bunto(:force_build => true,
40
+ :source => @sourcedir,
41
+ :destination => @destdir)
42
+ file_must_exist(@page)
43
+ end
44
+ end
45
+
46
+ describe "when empty destination directory exists" do
47
+
48
+ it "should build the site" do
49
+ file_wont_exist(@page)
50
+ rack_bunto(:source => @sourcedir,
51
+ :destination => @destdir)
52
+ file_must_exist(@page)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,110 @@
1
+ require_relative "helper"
2
+
3
+
4
+ describe "when configuring site" do
5
+
6
+ before do
7
+ @tempdir = File.join(TEST_DIR, "tmp")
8
+ FileUtils.mkdir_p(@tempdir) unless File.exist?(@tempdir)
9
+ Dir.chdir(@tempdir)
10
+ end
11
+
12
+ after do
13
+ FileUtils.rmdir(@tempdir) if File.exist?(@tempdir)
14
+ end
15
+
16
+ describe "when no options are given and no config file exists" do
17
+
18
+ it "loads the correct default destination" do
19
+ bunto = rack_bunto_without_build
20
+ bunto.destination.must_equal File.join(Dir.pwd, "_site")
21
+ end
22
+ end
23
+
24
+ describe "when using default config file" do
25
+
26
+ it "loads the configuration from file" do
27
+ begin
28
+ File.open("_config.yml", "w") do |f|
29
+ f.puts "config_file_opt: ok"
30
+ end
31
+
32
+ bunto = rack_bunto_without_build
33
+ bunto.config.must_include "config_file_opt"
34
+ bunto.config["config_file_opt"].must_equal "ok"
35
+ ensure
36
+ FileUtils.rm("_config.yml")
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "when using custom config file" do
42
+
43
+ it "loads the configuration from file" do
44
+ begin
45
+ File.open("_my_config.yml", "w") do |f|
46
+ f.puts "config_file_opt: ok"
47
+ end
48
+
49
+ bunto = rack_bunto_without_build(:config => "_my_config.yml")
50
+ bunto.config.must_include "config_file_opt"
51
+ bunto.config["config_file_opt"].must_equal "ok"
52
+ ensure
53
+ FileUtils.rm("_my_config.yml")
54
+ end
55
+ end
56
+ end
57
+
58
+ describe "when initialization options are given" do
59
+
60
+ it "has the initialization options" do
61
+ bunto = rack_bunto_without_build(:init_opt => "ok")
62
+ bunto.config.must_include "init_opt"
63
+ bunto.config["init_opt"].must_equal "ok"
64
+ end
65
+
66
+ it "has the correct destination" do
67
+ bunto = rack_bunto_without_build(:destination => "/project/_site")
68
+ bunto.destination.must_equal "/project/_site"
69
+ end
70
+
71
+ it ":auto is not passed on to Bunto" do
72
+ bunto = rack_bunto_without_build(:auto => "ok")
73
+ bunto.config.wont_include "auto"
74
+ end
75
+
76
+ it ":force_build is not passed on to Bunto" do
77
+ bunto = rack_bunto_without_build(:force_build => "ok")
78
+ bunto.config.wont_include "force_build"
79
+ end
80
+ end
81
+
82
+ describe "when initialization options are given and a config file exists" do
83
+
84
+ before do
85
+ File.open("_config.yml", "w") do |f|
86
+ f.puts "config_file_opt: ok"
87
+ f.puts "common_opt: from config"
88
+ f.puts "destination: /project/_site_from_config"
89
+ end
90
+ end
91
+
92
+ after do
93
+ FileUtils.rm("_config.yml")
94
+ end
95
+
96
+ it "has all options and initialization options override file options" do
97
+ bunto = rack_bunto_without_build(:init_opt => "ok",
98
+ :common_opt => "from init")
99
+ bunto.config.must_include "init_opt"
100
+ bunto.config.must_include "config_file_opt"
101
+ bunto.config.must_include "common_opt"
102
+ bunto.config["common_opt"].must_equal "from init"
103
+ end
104
+
105
+ it "has the correct destination" do
106
+ bunto = rack_bunto_without_build(:destination => "/project/_site_from_init")
107
+ bunto.destination.must_equal "/project/_site_from_init"
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,91 @@
1
+ require_relative "helper"
2
+
3
+
4
+ describe Rack::Bunto::FileHandler do
5
+
6
+ describe "when asked for filenames with #get_filename" do
7
+
8
+ before do
9
+ files = [
10
+ "/site/index.html",
11
+ "/site/page.html",
12
+ "/site/README",
13
+ "/site/dir-with-index/index.html",
14
+ "/site/dir-without-index/page.html",
15
+ "/site/dir1/dir2/dir3/index.html"
16
+ ]
17
+ @filehandler = Rack::Bunto::FileHandler.new("/site", files)
18
+ end
19
+
20
+ it "should return path for '/'" do
21
+ @filehandler.get_filename("/").must_equal "/site/index.html"
22
+ end
23
+
24
+ it "should return existing path" do
25
+ @filehandler.get_filename("/page.html").must_equal "/site/page.html"
26
+ end
27
+
28
+ it "should return nil for non-existent path" do
29
+ @filehandler.get_filename("/not-a-page.html").must_be_nil
30
+ end
31
+
32
+ it "should return existing path for resource without extension" do
33
+ @filehandler.get_filename("/README").must_equal "/site/README"
34
+ end
35
+
36
+ it "should return nil for partially matching paths" do
37
+ @filehandler.get_filename("/dir1/dir2/").must_be_nil
38
+ @filehandler.get_filename("/dir2/dir3").must_be_nil
39
+ @filehandler.get_filename("ir1/di").must_be_nil
40
+ end
41
+
42
+ it "should return path for directory/ with index" do
43
+ @filehandler.get_filename("/dir-with-index/").must_equal "/site/dir-with-index/index.html"
44
+ end
45
+
46
+ it "should return path for directory with index" do
47
+ @filehandler.get_filename("/dir-with-index").must_equal "/site/dir-with-index/index.html"
48
+ end
49
+
50
+ it "should return nil for directory/ without index" do
51
+ @filehandler.get_filename("/dir-without-index/").must_be_nil
52
+ end
53
+
54
+ it "should return nil for directory without index" do
55
+ @filehandler.get_filename("/dir-without-index").must_be_nil
56
+ end
57
+ end
58
+
59
+
60
+ describe "when retrieving the file list from root directory" do
61
+
62
+ before do
63
+ sourcedir = File.join(TEST_DIR, "source")
64
+ @filehandler = Rack::Bunto::FileHandler.new(sourcedir)
65
+ @existing_file = File.join(sourcedir, "index.md")
66
+ @existing_dir = File.join(sourcedir, "css")
67
+ end
68
+
69
+ it "should include regular files" do
70
+ @filehandler.files.must_include @existing_file
71
+ end
72
+
73
+ it "should not include directories" do
74
+ @filehandler.files.wont_include @existing_dir
75
+ end
76
+ end
77
+
78
+
79
+ describe "when initialized" do
80
+
81
+ it "should strip trailing slash from root" do
82
+ filehandler = Rack::Bunto::FileHandler.new("/site/", [])
83
+ filehandler.root.must_equal "/site"
84
+ end
85
+
86
+ it "should not append a trailing slash to root" do
87
+ filehandler = Rack::Bunto::FileHandler.new("/site", [])
88
+ filehandler.root.must_equal "/site"
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,179 @@
1
+ # encoding: UTF-8
2
+
3
+ require_relative "helper"
4
+
5
+
6
+ describe "when handling requests" do
7
+
8
+ before do
9
+ @tempdir = File.join(TEST_DIR, "tmp")
10
+ FileUtils.mkdir_p(@tempdir) unless File.exist?(@tempdir)
11
+ Dir.chdir(@tempdir)
12
+
13
+ @sourcedir = File.join(TEST_DIR, "source")
14
+ @destdir = File.join(@tempdir, "_site")
15
+ FileUtils.mkdir_p(@sourcedir) unless File.exist?(@sourcedir)
16
+ FileUtils.mkdir_p(@destdir) unless File.exist?(@destdir)
17
+
18
+ @bunto = rack_bunto(:force_build => true,
19
+ :source => @sourcedir,
20
+ :destination => @destdir)
21
+ @request = Rack::MockRequest.new(@bunto)
22
+ end
23
+
24
+ after do
25
+ FileUtils.rm_rf(@tempdir) if File.exist?(@tempdir)
26
+ end
27
+
28
+ it "can be created" do
29
+ @bunto.wont_be_nil
30
+ @request.wont_be_nil
31
+ end
32
+
33
+ it "should return status 200 for existing path" do
34
+ @request.get("/").status.must_equal 200
35
+ @request.get("/2015/10/05/hello-world.html").status.must_equal 200
36
+ end
37
+
38
+ it "should return status 404 for nonexistent path" do
39
+ @request.get("/show/me/404").status.must_equal 404
40
+ end
41
+
42
+ it "should return status 404 for partially matching path 1" do
43
+ @request.get("/2015/10/05/hello").status.must_equal 404
44
+ end
45
+
46
+ it "should return status 404 for partially matching path 2" do
47
+ @request.get("/10/05/hello-world.html").status.must_equal 404
48
+ end
49
+
50
+ it "should return correct status code for If-Modified-Since" do
51
+ modify_time = @request.get("/").headers["Last-Modified"]
52
+ earlier_time = (Time.parse(modify_time) - 3600).httpdate
53
+
54
+ @request.get("/", {"HTTP_IF_MODIFIED_SINCE" => modify_time}).status.must_equal 304
55
+ @request.get("/", {"HTTP_IF_MODIFIED_SINCE" => earlier_time}).status.must_equal 200
56
+ end
57
+
58
+ it "should return correct status code for If-Modified-Since with 404s" do
59
+ modify_time = @request.get("/").headers["Last-Modified"]
60
+ earlier_time = (Time.parse(modify_time) - 3600).httpdate
61
+
62
+ @request.get("/show/me/404", {"HTTP_IF_MODIFIED_SINCE" => earlier_time}).status.must_equal 404
63
+ end
64
+
65
+ it "should return correct Content-Type header" do
66
+ @request.get("/").headers["Content-Type"].must_equal "text/html"
67
+ @request.get("/css/test.css").headers["Content-Type"].must_equal "text/css"
68
+ @request.get("/js/test.js").headers["Content-Type"].must_equal "application/javascript"
69
+ @request.get("/js/test.min.js").headers["Content-Type"].must_equal "application/javascript"
70
+ @request.get("/show/me/404").headers["Content-Type"].must_equal "text/html"
71
+ end
72
+
73
+ it "should return correct Content-Length header for '/'" do
74
+ @request.get("/").original_headers["Content-Length"].to_i.must_equal 24
75
+ end
76
+
77
+ it "should return correct Content-Length header for If-Modified-Since" do
78
+ modify_time = @request.get("/").headers["Last-Modified"]
79
+ @request.get("/", {"HTTP_IF_MODIFIED_SINCE" => modify_time}).original_headers["Content-Length"].must_be_nil
80
+ end
81
+
82
+ it "should return correct body for '/'" do
83
+ @request.get("/").body.must_equal "<p>Rack-Bunto Test</p>\n"
84
+ end
85
+
86
+
87
+ describe "when a directory is requested" do
88
+
89
+ it "should redirect with status 301 to 'directory/' for 'directory' with index.html" do
90
+ skip
91
+ @request.get("/dir-with-index").status.must_equal 301
92
+ @request.get("/dir-with-index").headers["Location"].must_equal "/dir-with-index/"
93
+ end
94
+
95
+ it "should return status 200 for 'directory/' with index.html" do
96
+ @request.get("/dir-with-index/").status.must_equal 200
97
+ end
98
+
99
+ it "should return status 404 for 'directory' without index.html" do
100
+ @request.get("/dir-without-index").status.must_equal 404
101
+ end
102
+
103
+ it "should return status 404 for 'directory/' without index.html" do
104
+ @request.get("/dir-without-index/").status.must_equal 404
105
+ end
106
+ end
107
+
108
+
109
+ describe "when a resource of unknown mime type is requested" do
110
+
111
+ it "should return status 200" do
112
+ @request.get("/no-extension").status.must_equal 200
113
+ end
114
+
115
+ it "should return Content-Type 'application/octet-stream'" do
116
+ @request.get("/no-extension").headers["Content-Type"].must_equal "application/octet-stream"
117
+ end
118
+ end
119
+
120
+
121
+ describe "when page contains multibyte characters" do
122
+
123
+ before do
124
+ @response = @request.get("/buenos_dias.html")
125
+ end
126
+
127
+ it "should return correct body" do
128
+ @response.body.must_equal "<p>¡Buenos días!</p>\n"
129
+ end
130
+
131
+ it "should return the bytesize as Content-Length header" do
132
+ @response.original_headers["Content-Length"].to_i.must_equal 23
133
+ end
134
+ end
135
+
136
+
137
+ describe "when returning 404s" do
138
+
139
+ it "should return correct body for default 404" do
140
+ @request.get("/show/me/404").body.must_equal "Not found"
141
+ end
142
+
143
+ it "should return correct Content-Length header for default 404" do
144
+ @request.get("/show/me/404").original_headers["Content-Length"].to_i.must_equal 9
145
+ end
146
+
147
+ it "should return correct body for custom 404" do
148
+ begin
149
+ filename = File.join(@sourcedir, "404.html")
150
+ File.open(filename, "w") {|f| f.puts "Custom 404" }
151
+
152
+ bunto = rack_bunto(:force_build => true,
153
+ :source => @sourcedir,
154
+ :destination => @destdir)
155
+ request = Rack::MockRequest.new(bunto)
156
+
157
+ request.get("/show/me/404").body.must_match %r{Custom 404}
158
+ ensure
159
+ FileUtils.rm(filename)
160
+ end
161
+ end
162
+ end
163
+
164
+
165
+ describe "when handling HEAD requests" do
166
+
167
+ it "should return status 200 for '/'" do
168
+ @request.head("/").status.must_equal 200
169
+ end
170
+
171
+ it "should return correct Content-Length header for '/'" do
172
+ @request.head("/").original_headers["Content-Length"].to_i.must_equal 24
173
+ end
174
+
175
+ it "should not return a body" do
176
+ @request.head("/").body.must_equal ""
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,54 @@
1
+ require_relative "helper"
2
+
3
+
4
+ describe Rack::Bunto::Utils do
5
+
6
+ before do
7
+ @utils = Rack::Bunto::Utils
8
+ end
9
+
10
+
11
+ describe "when asked for #media_type" do
12
+
13
+ it "returns correct type for HTML" do
14
+ @utils.media_type("index.html").must_equal "text/html"
15
+ end
16
+
17
+ it "returns correct type for HTML with full path" do
18
+ @utils.media_type("/path/index.html").must_equal "text/html"
19
+ end
20
+
21
+ it "returns correct type for CSS" do
22
+ @utils.media_type("/path/style.css").must_equal "text/css"
23
+ end
24
+
25
+ it "returns correct type for *.min.js" do
26
+ @utils.media_type("script.min.js").must_equal "application/javascript"
27
+ end
28
+
29
+ it "returns correct default type for files without extension" do
30
+ @utils.media_type("README").must_equal "application/octet-stream"
31
+ end
32
+
33
+ it "ignores lower/upper case in file extensions" do
34
+ @utils.media_type("image.JpG").must_equal "image/jpeg"
35
+ end
36
+ end
37
+
38
+
39
+ describe "when asked for #file_info" do
40
+
41
+ before do
42
+ @filename = __FILE__
43
+ @info = @utils.file_info(@filename)
44
+ end
45
+
46
+ it "has the file content" do
47
+ @info[:body].must_equal ::File.read(@filename)
48
+ end
49
+
50
+ it "has the modification time in httpdate format" do
51
+ @info[:time].must_equal ::File.mtime(@filename).httpdate
52
+ end
53
+ end
54
+ end