title: How do I test my JSON API with Sinatra + rspec
body: <p>I have a post method that accepts JSON:</p>
  
  <pre><code>post '/channel/create' do
    content_type :json
  
    @data = JSON.parse(env['rack.input'].gets)
  
    if @data.nil? or !@data.has_key?('api_key')
      status 400
      body({ :error =&gt; "JSON corrupt" }.to_json)
    else
      status 200
      body({ :error =&gt; "Channel created" }.to_json)
    end
  </code></pre>
  
  <p>As a newbie to rspec I am bewildered trying to figure out how to write a test against that POST with an acceptable JSON payload.  The closest I got to is this which is woefully inaccurate but I don't seem to be asking the Google god the right questions to help me out here.</p>
  
  <pre><code>  it "accepts create channel" do
      h = {'Content-Type' =&gt; 'application/json'}
      body = { :key =&gt; "abcdef" }.to_json
      post '/channel/create', body, h
      last_response.should be_ok
    end
  </code></pre>
  
  <p>Any best practice guidance for testing APIs in Sinatra will be most appreciated also.</p>