File: body_parse_errors_spec.rb

package info (click to toggle)
ruby-grape 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,156 kB
  • sloc: ruby: 25,265; makefile: 7
file content (147 lines) | stat: -rw-r--r-- 4,274 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# frozen_string_literal: true

require 'spec_helper'

describe Grape::Exceptions::ValidationErrors do
  context 'api with rescue_from :all handler' do
    subject { Class.new(Grape::API) }

    before do
      subject.rescue_from :all do |_e|
        rack_response 'message was processed', 400
      end
      subject.params do
        requires :beer
      end
      subject.post '/beer' do
        'beer received'
      end
    end

    def app
      subject
    end

    context 'with content_type json' do
      it 'can recover from failed body parsing' do
        post '/beer', 'test', 'CONTENT_TYPE' => 'application/json'
        expect(last_response.status).to eq 400
        expect(last_response.body).to eq('message was processed')
      end
    end

    context 'with content_type xml' do
      it 'can recover from failed body parsing' do
        post '/beer', 'test', 'CONTENT_TYPE' => 'application/xml'
        expect(last_response.status).to eq 400
        expect(last_response.body).to eq('message was processed')
      end
    end

    context 'with content_type text' do
      it 'can recover from failed body parsing' do
        post '/beer', 'test', 'CONTENT_TYPE' => 'text/plain'
        expect(last_response.status).to eq 400
        expect(last_response.body).to eq('message was processed')
      end
    end

    context 'with no specific content_type' do
      it 'can recover from failed body parsing' do
        post '/beer', 'test', {}
        expect(last_response.status).to eq 400
        expect(last_response.body).to eq('message was processed')
      end
    end
  end

  context 'api with rescue_from :grape_exceptions handler' do
    subject { Class.new(Grape::API) }

    before do
      subject.rescue_from :all do |_e|
        rack_response 'message was processed', 400
      end
      subject.rescue_from :grape_exceptions

      subject.params do
        requires :beer
      end
      subject.post '/beer' do
        'beer received'
      end
    end

    def app
      subject
    end

    context 'with content_type json' do
      it 'returns body parsing error message' do
        post '/beer', 'test', 'CONTENT_TYPE' => 'application/json'
        expect(last_response.status).to eq 400
        expect(last_response.body).to include 'message body does not match declared format'
      end
    end

    context 'with content_type xml' do
      it 'returns body parsing error message' do
        post '/beer', 'test', 'CONTENT_TYPE' => 'application/xml'
        expect(last_response.status).to eq 400
        expect(last_response.body).to include 'message body does not match declared format'
      end
    end
  end

  context 'api without a rescue handler' do
    subject { Class.new(Grape::API) }

    before do
      subject.params do
        requires :beer
      end
      subject.post '/beer' do
        'beer received'
      end
    end

    def app
      subject
    end

    context 'and with content_type json' do
      it 'can recover from failed body parsing' do
        post '/beer', 'test', 'CONTENT_TYPE' => 'application/json'
        expect(last_response.status).to eq 400
        expect(last_response.body).to include('message body does not match declared format')
        expect(last_response.body).to include('application/json')
      end
    end

    context 'with content_type xml' do
      it 'can recover from failed body parsing' do
        post '/beer', 'test', 'CONTENT_TYPE' => 'application/xml'
        expect(last_response.status).to eq 400
        expect(last_response.body).to include('message body does not match declared format')
        expect(last_response.body).to include('application/xml')
      end
    end

    context 'with content_type text' do
      it 'can recover from failed body parsing' do
        post '/beer', 'test', 'CONTENT_TYPE' => 'text/plain'
        expect(last_response.status).to eq 400
        expect(last_response.body).to eq('beer is missing')
      end
    end

    context 'and with no specific content_type' do
      it 'can recover from failed body parsing' do
        post '/beer', 'test', {}
        expect(last_response.status).to eq 400
        # plain response with text/html
        expect(last_response.body).to eq('beer is missing')
      end
    end
  end
end