-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spike towards server-controlled immediate printing.
This relates to #8, and explores using a single encoded header to simplify parsing of more complex parameters from the server.
- Loading branch information
Showing
5 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
lib/printer/backend_server/encoded_status_header_middleware.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Printer::BackendServer::EncodedStatusHeaderMiddleware | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
status, headers, body = @app.call(env) | ||
headers.merge!("X-Printer-Encoded-Status" => encoded_status(status, headers)) | ||
[status, headers, body] | ||
end | ||
|
||
private | ||
|
||
def encoded_status(status, headers) | ||
[status, headers["Content-length"], encoded_presence(headers["X-Printer-PrintImmediately"])].compact.join("|") | ||
end | ||
|
||
def encoded_presence(header) | ||
header ? 1 : 0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
test/backend_server/encoded_status_header_middleware_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require "test_helper" | ||
require "rack/test" | ||
require "printer/backend_server" | ||
|
||
ENV['RACK_ENV'] = 'test' | ||
|
||
describe Printer::BackendServer::EncodedStatusHeaderMiddleware do | ||
include Rack::Test::Methods | ||
|
||
def app | ||
Rack::Builder.new do | ||
use Printer::BackendServer::EncodedStatusHeaderMiddleware | ||
use Rack::ContentLength | ||
run lambda { |env| | ||
headers = env["PATH_INFO"] == "/print-now" ? {"X-Printer-PrintImmediately" => true} : {} | ||
[200, headers, ["Response body"]] | ||
} | ||
end | ||
end | ||
|
||
describe "making a request" do | ||
before do | ||
get "anything" | ||
end | ||
|
||
it "includes the encoded header" do | ||
assert last_response.headers.keys.include?("X-Printer-Encoded-Status") | ||
end | ||
|
||
it "encodes the status in the header" do | ||
last_response.headers["X-Printer-Encoded-Status"].split("|")[0].must_equal "200" | ||
end | ||
|
||
it "encodes the content length in the header" do | ||
last_response.headers["X-Printer-Encoded-Status"].split("|")[1].must_equal "Response body".length.to_s | ||
end | ||
end | ||
|
||
describe "making a request where the X-Printer-PrintImmediately header is present" do | ||
it "encodes the print immediately value into the header" do | ||
get "/print-now" | ||
last_response.headers["X-Printer-Encoded-Status"].split("|")[2].must_equal "1" | ||
end | ||
end | ||
end |