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

added reload pipe and specs back #960

Closed
wants to merge 1 commit into from
Closed
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
94 changes: 47 additions & 47 deletions spec/amber/pipes/reload_spec.cr
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
# TODO: remove this file after https://github.com/amberframework/amber/pull/860 is merged

# class FakeEnvironment < Amber::Environment::Env
# def development?
# true
# end
# end

# module Amber
# module Pipe
# describe Reload do
# headers = HTTP::Headers.new
# headers["Accept"] = "text/html"
# request = HTTP::Request.new("GET", "/reload", headers)

# Amber::Server.router.draw :web do
# get "/reload", HelloController, :index
# end

# context "when environment is in development mode" do
# pipeline = Pipeline.new
# pipeline.build :web do
# plug Amber::Pipe::Reload.new(FakeEnvironment.new)
# end
# pipeline.prepare_pipelines

# it "contains injected code in response.body" do
# response = create_request_and_return_io(pipeline, request)

# response.body.should contain "Code injected by Amber Framework"
# end
# end

# context "when environment is NOT in development mode" do
# pipeline = Pipeline.new
# pipeline.build :web do
# plug Amber::Pipe::Reload.new
# end
# pipeline.prepare_pipelines

# it "does not have injected reload code in response.body" do
# response = create_request_and_return_io(pipeline, request)

# response.body.should_not contain "Code injected by Amber Framework"
# end
# end
# end
# end
# end
class FakeEnvironment < Amber::Environment::Env
def development?
true
end
end

module Amber
module Pipe
describe Reload do
headers = HTTP::Headers.new
headers["Accept"] = "text/html"
request = HTTP::Request.new("GET", "/reload", headers)

Amber::Server.router.draw :web do
get "/reload", HelloController, :index
end

context "when environment is in development mode" do
pipeline = Pipeline.new
pipeline.build :web do
plug Amber::Pipe::Reload.new(FakeEnvironment.new)
end
pipeline.prepare_pipelines

it "contains injected code in response.body" do
response = create_request_and_return_io(pipeline, request)

response.body.should contain "Code injected by Amber Framework"
end
end

context "when environment is NOT in development mode" do
pipeline = Pipeline.new
pipeline.build :web do
plug Amber::Pipe::Reload.new
end
pipeline.prepare_pipelines

it "does not have injected reload code in response.body" do
response = create_request_and_return_io(pipeline, request)

response.body.should_not contain "Code injected by Amber Framework"
end
end
end
end
end
2 changes: 2 additions & 0 deletions src/amber/cli/templates/app/config/routes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Amber::Server.configure do
plug Amber::Pipe::Session.new
plug Amber::Pipe::Flash.new
plug Amber::Pipe::CSRF.new
# Reload clients browsers (development only)
plug Amber::Pipe::Reload.new if Amber.env.development?
end

pipeline :api do
Expand Down
26 changes: 26 additions & 0 deletions src/amber/pipes/reload.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "../../support/client_reload"

module Amber
module Pipe
# Reload clients browsers using `ClientReload`.
#
# NOTE: Amber::Pipe::Reload is intended for use in a development environment.
# ```
# pipeline :web do
# plug Amber::Pipe::Reload.new
# end
# ```
class Reload < Base
def initialize(@env : Amber::Environment::Env = Amber.env)
Support::ClientReload.new
end

def call(context : HTTP::Server::Context)
if @env.development? && context.format == "html"
context.response << Support::ClientReload::INJECTED_CODE
end
call_next(context)
end
end
end
end