From 24b1be98470b70f835baad554c7fad3bdb696b32 Mon Sep 17 00:00:00 2001 From: Isaac Sloan Date: Sat, 13 Oct 2018 22:00:03 -0700 Subject: [PATCH] added reload pipe and specs back --- spec/amber/pipes/reload_spec.cr | 94 ++++++++++---------- src/amber/cli/templates/app/config/routes.cr | 2 + src/amber/pipes/reload.cr | 26 ++++++ 3 files changed, 75 insertions(+), 47 deletions(-) create mode 100644 src/amber/pipes/reload.cr diff --git a/spec/amber/pipes/reload_spec.cr b/spec/amber/pipes/reload_spec.cr index c9cfd3501..dce00dc11 100644 --- a/spec/amber/pipes/reload_spec.cr +++ b/spec/amber/pipes/reload_spec.cr @@ -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 diff --git a/src/amber/cli/templates/app/config/routes.cr b/src/amber/cli/templates/app/config/routes.cr index 0c9e0e3f3..f016ba192 100644 --- a/src/amber/cli/templates/app/config/routes.cr +++ b/src/amber/cli/templates/app/config/routes.cr @@ -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 diff --git a/src/amber/pipes/reload.cr b/src/amber/pipes/reload.cr new file mode 100644 index 000000000..26effc5cf --- /dev/null +++ b/src/amber/pipes/reload.cr @@ -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