Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
Repaired Rails params spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
nning committed Feb 3, 2015
1 parent 17d3839 commit 2680a25
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 34 deletions.
11 changes: 4 additions & 7 deletions lib/david/server/respond.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ def respond(exchange, env = nil)
body = body_to_json(cbor)
body = body.force_encoding('ASCII-8BIT') # Rack::Lint insisted...

env[COAP_CBOR] = cbor
env[CONTENT_TYPE] = CONTENT_TYPE_JSON
env[RACK_INPUT] = StringIO.new(body)
env[COAP_CBOR] = cbor
env[CONTENT_LENGTH] = body.bytesize
env[CONTENT_TYPE] = CONTENT_TYPE_JSON
env[RACK_INPUT] = StringIO.new(body)
rescue EOFError, CBOR::MalformedFormatError
end
end
Expand All @@ -48,8 +49,6 @@ def respond(exchange, env = nil)
ct = headers[HTTP_CONTENT_TYPE]
body = body_to_string(body)

body.close if body.respond_to?(:close)

if @options[:CBOR] && ct == CONTENT_TYPE_JSON
begin
body = body_to_cbor(body)
Expand All @@ -66,7 +65,6 @@ def respond(exchange, env = nil)
loc = location_to_coap(headers)
ma = max_age_to_coap(headers)
mcode = code_to_coap(code)
size = headers[HTTP_CONTENT_LENGTH].to_i

# App returned cf different from accept
return error(exchange, 4.06) if exchange.accept && exchange.accept != cf
Expand All @@ -88,7 +86,6 @@ def respond(exchange, env = nil)

response.payload = block.chunk(body)
response.options[:block2] = block.encode
# response.options[:size2] = size if size != 0
else
response.payload = body
end
Expand Down
1 change: 1 addition & 0 deletions lib/david/server/utility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Utility
def body_to_string(body)
s = ''
body.each { |line| s << line << "\r\n" }
body.close if body.respond_to?(:close)
s.chomp
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ def benchmark
end

def cbor
render text: params.to_s
render text: params['test'].to_s
end
end
4 changes: 2 additions & 2 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Rails.application.routes.draw do
resources :things

get 'hello' => 'tests#benchmark'
post 'cbor' => 'tests#cbor'
get 'hello' => 'tests#benchmark'
get 'cbor' => 'tests#cbor'
end
42 changes: 18 additions & 24 deletions spec/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,12 @@
context 'transcoding' do
let!(:server) { supervised_server(:Port => port, :CBOR => true) }

let(:cbor) { {'Hello' => 'World!'}.to_cbor }

subject { client.get('/cbor', '::1', nil, cbor, content_format: 60) }

context 'incoming' do
context 'string key' do
let(:cbor) { {'Hello' => 'World!'}.to_cbor }

it 'should return text' do
expect(subject).to be_a(CoAP::Message)
expect(subject.ver).to eq(1)
Expand All @@ -316,28 +316,22 @@
end

context 'rails' do
pending

# let!(:server) do
# supervised_server({
# :Port => port,
# :Log => debug,
# :CBOR => true,
# :app => Dummy::Application
# })
# end

# let(:cbor) { {'test' => {'Hello' => 'World!'}}.to_cbor }

# subject { client.post('/cbor', '::1', nil, cbor, content_format: 60) }

# it 'should return text' do
# expect(subject).to be_a(CoAP::Message)
# expect(subject.ver).to eq(1)
# expect(subject.tt).to eq(:ack)
# expect(subject.mcode).to eq([2, 5])
# expect(subject.payload).to eq('{"Hello"=>"World!"}')
# end
let!(:server) do
supervised_server({
:Port => port,
:Log => debug,
:CBOR => true,
:app => Rails.application
})
end

it 'should return text' do
expect(subject).to be_a(CoAP::Message)
expect(subject.ver).to eq(1)
expect(subject.tt).to eq(:ack)
expect(subject.mcode).to eq([2, 5])
expect(subject.payload).to eq('{"Hello"=>"World!"}')
end
end
end

Expand Down

0 comments on commit 2680a25

Please sign in to comment.