grape 1.5.3 → 1.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +39 -0
- data/CONTRIBUTING.md +2 -1
- data/README.md +31 -3
- data/UPGRADING.md +46 -4
- data/grape.gemspec +5 -5
- data/lib/grape/api/instance.rb +13 -17
- data/lib/grape/api.rb +17 -12
- data/lib/grape/cookies.rb +2 -0
- data/lib/grape/dsl/desc.rb +3 -5
- data/lib/grape/dsl/headers.rb +5 -2
- data/lib/grape/dsl/helpers.rb +7 -5
- data/lib/grape/dsl/inside_route.rb +17 -8
- data/lib/grape/dsl/middleware.rb +4 -4
- data/lib/grape/dsl/parameters.rb +3 -3
- data/lib/grape/dsl/request_response.rb +9 -6
- data/lib/grape/dsl/routing.rb +2 -2
- data/lib/grape/dsl/settings.rb +5 -5
- data/lib/grape/endpoint.rb +20 -35
- data/lib/grape/error_formatter/json.rb +2 -6
- data/lib/grape/error_formatter/xml.rb +2 -6
- data/lib/grape/exceptions/validation.rb +1 -2
- data/lib/grape/formatter/json.rb +1 -0
- data/lib/grape/formatter/serializable_hash.rb +2 -1
- data/lib/grape/formatter/xml.rb +1 -0
- data/lib/grape/middleware/auth/dsl.rb +7 -1
- data/lib/grape/middleware/base.rb +3 -1
- data/lib/grape/middleware/formatter.rb +4 -4
- data/lib/grape/middleware/stack.rb +2 -2
- data/lib/grape/middleware/versioner/accept_version_header.rb +3 -5
- data/lib/grape/middleware/versioner/header.rb +6 -4
- data/lib/grape/middleware/versioner/param.rb +1 -0
- data/lib/grape/middleware/versioner/parse_media_type_patch.rb +2 -1
- data/lib/grape/middleware/versioner/path.rb +2 -0
- data/lib/grape/path.rb +1 -0
- data/lib/grape/request.rb +1 -0
- data/lib/grape/router/pattern.rb +1 -1
- data/lib/grape/router/route.rb +2 -2
- data/lib/grape/router.rb +6 -0
- data/lib/grape/util/inheritable_setting.rb +1 -3
- data/lib/grape/util/lazy_value.rb +3 -2
- data/lib/grape/util/strict_hash_configuration.rb +1 -1
- data/lib/grape/validations/params_scope.rb +88 -55
- data/lib/grape/validations/types/custom_type_coercer.rb +1 -0
- data/lib/grape/validations/types/dry_type_coercer.rb +1 -1
- data/lib/grape/validations/types/json.rb +2 -1
- data/lib/grape/validations/types/primitive_coercer.rb +3 -3
- data/lib/grape/validations/validators/all_or_none.rb +8 -5
- data/lib/grape/validations/validators/allow_blank.rb +9 -7
- data/lib/grape/validations/validators/as.rb +6 -8
- data/lib/grape/validations/validators/at_least_one_of.rb +7 -4
- data/lib/grape/validations/validators/base.rb +75 -70
- data/lib/grape/validations/validators/coerce.rb +63 -79
- data/lib/grape/validations/validators/default.rb +37 -34
- data/lib/grape/validations/validators/exactly_one_of.rb +9 -6
- data/lib/grape/validations/validators/except_values.rb +13 -11
- data/lib/grape/validations/validators/multiple_params_base.rb +24 -20
- data/lib/grape/validations/validators/mutual_exclusion.rb +8 -5
- data/lib/grape/validations/validators/presence.rb +7 -4
- data/lib/grape/validations/validators/regexp.rb +8 -5
- data/lib/grape/validations/validators/same_as.rb +18 -15
- data/lib/grape/validations/validators/values.rb +61 -56
- data/lib/grape/validations.rb +6 -0
- data/lib/grape/version.rb +1 -1
- data/lib/grape.rb +3 -1
- data/spec/grape/api/custom_validations_spec.rb +77 -45
- data/spec/grape/api/deeply_included_options_spec.rb +3 -3
- data/spec/grape/api/defines_boolean_in_params_spec.rb +2 -1
- data/spec/grape/api/invalid_format_spec.rb +2 -0
- data/spec/grape/api/recognize_path_spec.rb +1 -1
- data/spec/grape/api/routes_with_requirements_spec.rb +8 -8
- data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +9 -15
- data/spec/grape/api_remount_spec.rb +16 -15
- data/spec/grape/api_spec.rb +440 -227
- data/spec/grape/dsl/callbacks_spec.rb +2 -1
- data/spec/grape/dsl/headers_spec.rb +39 -9
- data/spec/grape/dsl/helpers_spec.rb +3 -2
- data/spec/grape/dsl/inside_route_spec.rb +6 -4
- data/spec/grape/dsl/logger_spec.rb +16 -18
- data/spec/grape/dsl/middleware_spec.rb +2 -1
- data/spec/grape/dsl/parameters_spec.rb +2 -0
- data/spec/grape/dsl/request_response_spec.rb +1 -0
- data/spec/grape/dsl/routing_spec.rb +10 -7
- data/spec/grape/endpoint/declared_spec.rb +259 -12
- data/spec/grape/endpoint_spec.rb +64 -55
- data/spec/grape/entity_spec.rb +22 -22
- data/spec/grape/exceptions/body_parse_errors_spec.rb +3 -0
- data/spec/grape/exceptions/invalid_accept_header_spec.rb +61 -22
- data/spec/grape/exceptions/validation_errors_spec.rb +13 -10
- data/spec/grape/exceptions/validation_spec.rb +5 -3
- data/spec/grape/extensions/param_builders/hash_spec.rb +7 -7
- data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +8 -8
- data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +8 -8
- data/spec/grape/integration/rack_sendfile_spec.rb +1 -1
- data/spec/grape/loading_spec.rb +8 -8
- data/spec/grape/middleware/auth/dsl_spec.rb +15 -6
- data/spec/grape/middleware/auth/strategies_spec.rb +60 -20
- data/spec/grape/middleware/base_spec.rb +24 -15
- data/spec/grape/middleware/error_spec.rb +2 -2
- data/spec/grape/middleware/exception_spec.rb +111 -161
- data/spec/grape/middleware/formatter_spec.rb +27 -6
- data/spec/grape/middleware/globals_spec.rb +7 -4
- data/spec/grape/middleware/stack_spec.rb +14 -12
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +2 -1
- data/spec/grape/middleware/versioner/header_spec.rb +14 -13
- data/spec/grape/middleware/versioner/param_spec.rb +7 -1
- data/spec/grape/middleware/versioner/path_spec.rb +5 -1
- data/spec/grape/middleware/versioner_spec.rb +1 -1
- data/spec/grape/parser_spec.rb +4 -0
- data/spec/grape/path_spec.rb +52 -52
- data/spec/grape/presenters/presenter_spec.rb +7 -6
- data/spec/grape/request_spec.rb +6 -4
- data/spec/grape/util/inheritable_setting_spec.rb +7 -7
- data/spec/grape/util/inheritable_values_spec.rb +3 -2
- data/spec/grape/util/reverse_stackable_values_spec.rb +3 -1
- data/spec/grape/util/stackable_values_spec.rb +7 -5
- data/spec/grape/validations/instance_behaivour_spec.rb +9 -10
- data/spec/grape/validations/multiple_attributes_iterator_spec.rb +1 -0
- data/spec/grape/validations/params_scope_spec.rb +46 -10
- data/spec/grape/validations/single_attribute_iterator_spec.rb +2 -1
- data/spec/grape/validations/types/primitive_coercer_spec.rb +4 -4
- data/spec/grape/validations/types_spec.rb +8 -8
- data/spec/grape/validations/validators/all_or_none_spec.rb +50 -56
- data/spec/grape/validations/validators/allow_blank_spec.rb +136 -140
- data/spec/grape/validations/validators/at_least_one_of_spec.rb +50 -56
- data/spec/grape/validations/validators/coerce_spec.rb +23 -22
- data/spec/grape/validations/validators/default_spec.rb +72 -78
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +71 -77
- data/spec/grape/validations/validators/except_values_spec.rb +3 -3
- data/spec/grape/validations/validators/mutual_exclusion_spec.rb +71 -77
- data/spec/grape/validations/validators/presence_spec.rb +16 -1
- data/spec/grape/validations/validators/regexp_spec.rb +25 -31
- data/spec/grape/validations/validators/same_as_spec.rb +14 -20
- data/spec/grape/validations/validators/values_spec.rb +183 -178
- data/spec/grape/validations_spec.rb +99 -58
- data/spec/integration/eager_load/eager_load_spec.rb +2 -2
- data/spec/integration/multi_json/json_spec.rb +1 -1
- data/spec/integration/multi_xml/xml_spec.rb +1 -1
- data/spec/shared/versioning_examples.rb +12 -9
- data/spec/spec_helper.rb +12 -2
- data/spec/support/basic_auth_encode_helpers.rb +1 -1
- metadata +103 -103
@@ -489,17 +489,24 @@ describe Grape::Validations do
|
|
489
489
|
end
|
490
490
|
|
491
491
|
context 'custom validator for a Hash' do
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
end
|
492
|
+
let(:date_range_validator) do
|
493
|
+
Class.new(Grape::Validations::Validators::Base) do
|
494
|
+
def validate_param!(attr_name, params)
|
495
|
+
return if params[attr_name][:from] <= params[attr_name][:to]
|
496
|
+
|
497
|
+
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: "'from' must be lower or equal to 'to'")
|
499
498
|
end
|
500
499
|
end
|
501
500
|
end
|
502
501
|
|
502
|
+
before do
|
503
|
+
described_class.register_validator('date_range', date_range_validator)
|
504
|
+
end
|
505
|
+
|
506
|
+
after do
|
507
|
+
described_class.deregister_validator('date_range')
|
508
|
+
end
|
509
|
+
|
503
510
|
before do
|
504
511
|
subject.params do
|
505
512
|
optional :date_range, date_range: true, type: Hash do
|
@@ -887,13 +894,13 @@ describe Grape::Validations do
|
|
887
894
|
context <<~DESC do
|
888
895
|
Issue occurs whenever:
|
889
896
|
* param structure with at least three levels
|
890
|
-
* 1st level item is a required Array that has >1 entry with an optional item present and >1 entry with an optional item missing
|
891
|
-
* 2nd level is an optional Array or Hash
|
897
|
+
* 1st level item is a required Array that has >1 entry with an optional item present and >1 entry with an optional item missing#{' '}
|
898
|
+
* 2nd level is an optional Array or Hash#{' '}
|
892
899
|
* 3rd level is a required item (can be any type)
|
893
900
|
* additional levels do not effect the issue from occuring
|
894
901
|
DESC
|
895
902
|
|
896
|
-
it
|
903
|
+
it 'example based off actual real world use case' do
|
897
904
|
subject.params do
|
898
905
|
requires :orders, type: Array do
|
899
906
|
requires :id, type: Integer
|
@@ -911,17 +918,17 @@ describe Grape::Validations do
|
|
911
918
|
|
912
919
|
data = {
|
913
920
|
orders: [
|
914
|
-
{ id: 77, drugs: [{batches: [{batch_no:
|
921
|
+
{ id: 77, drugs: [{ batches: [{ batch_no: 'A1234567' }] }] },
|
915
922
|
{ id: 70 }
|
916
923
|
]
|
917
924
|
}
|
918
925
|
|
919
926
|
get '/validate_required_arrays_under_optional_arrays', data
|
920
|
-
expect(last_response.body).to eq(
|
927
|
+
expect(last_response.body).to eq('validate_required_arrays_under_optional_arrays works!')
|
921
928
|
expect(last_response.status).to eq(200)
|
922
929
|
end
|
923
930
|
|
924
|
-
it
|
931
|
+
it 'simplest example using Array -> Array -> Hash -> String' do
|
925
932
|
subject.params do
|
926
933
|
requires :orders, type: Array do
|
927
934
|
requires :id, type: Integer
|
@@ -937,17 +944,17 @@ describe Grape::Validations do
|
|
937
944
|
|
938
945
|
data = {
|
939
946
|
orders: [
|
940
|
-
{ id: 77, drugs: [{batch_no:
|
947
|
+
{ id: 77, drugs: [{ batch_no: 'A1234567' }] },
|
941
948
|
{ id: 70 }
|
942
949
|
]
|
943
950
|
}
|
944
951
|
|
945
952
|
get '/validate_required_arrays_under_optional_arrays', data
|
946
|
-
expect(last_response.body).to eq(
|
953
|
+
expect(last_response.body).to eq('validate_required_arrays_under_optional_arrays works!')
|
947
954
|
expect(last_response.status).to eq(200)
|
948
955
|
end
|
949
956
|
|
950
|
-
it
|
957
|
+
it 'simplest example using Array -> Hash -> String' do
|
951
958
|
subject.params do
|
952
959
|
requires :orders, type: Array do
|
953
960
|
requires :id, type: Integer
|
@@ -963,17 +970,17 @@ describe Grape::Validations do
|
|
963
970
|
|
964
971
|
data = {
|
965
972
|
orders: [
|
966
|
-
{ id: 77, drugs: {batch_no:
|
973
|
+
{ id: 77, drugs: { batch_no: 'A1234567' } },
|
967
974
|
{ id: 70 }
|
968
975
|
]
|
969
976
|
}
|
970
977
|
|
971
978
|
get '/validate_required_arrays_under_optional_arrays', data
|
972
|
-
expect(last_response.body).to eq(
|
979
|
+
expect(last_response.body).to eq('validate_required_arrays_under_optional_arrays works!')
|
973
980
|
expect(last_response.status).to eq(200)
|
974
981
|
end
|
975
982
|
|
976
|
-
it
|
983
|
+
it 'correctly indexes invalida data' do
|
977
984
|
subject.params do
|
978
985
|
requires :orders, type: Array do
|
979
986
|
requires :id, type: Integer
|
@@ -991,16 +998,16 @@ describe Grape::Validations do
|
|
991
998
|
data = {
|
992
999
|
orders: [
|
993
1000
|
{ id: 70 },
|
994
|
-
{ id: 77, drugs: [{batch_no:
|
1001
|
+
{ id: 77, drugs: [{ batch_no: 'A1234567', quantity: 12 }, { batch_no: 'B222222' }] }
|
995
1002
|
]
|
996
1003
|
}
|
997
1004
|
|
998
1005
|
get '/correctly_indexes', data
|
999
|
-
expect(last_response.body).to eq(
|
1006
|
+
expect(last_response.body).to eq('orders[1][drugs][1][quantity] is missing')
|
1000
1007
|
expect(last_response.status).to eq(400)
|
1001
1008
|
end
|
1002
1009
|
|
1003
|
-
context
|
1010
|
+
context 'multiple levels of optional and requires settings' do
|
1004
1011
|
before do
|
1005
1012
|
subject.params do
|
1006
1013
|
requires :top, type: Array do
|
@@ -1022,53 +1029,62 @@ describe Grape::Validations do
|
|
1022
1029
|
end
|
1023
1030
|
end
|
1024
1031
|
|
1025
|
-
it
|
1032
|
+
it 'with valid data' do
|
1026
1033
|
data = {
|
1027
1034
|
top: [
|
1028
1035
|
{ top_id: 1, middle_1: [
|
1029
|
-
{middle_1_id: 11}, {middle_1_id: 12, middle_2: [
|
1030
|
-
{middle_2_id: 121}, {middle_2_id: 122, bottom: [{bottom_id: 1221}]}
|
1036
|
+
{ middle_1_id: 11 }, { middle_1_id: 12, middle_2: [
|
1037
|
+
{ middle_2_id: 121 }, { middle_2_id: 122, bottom: [{ bottom_id: 1221 }] }
|
1038
|
+
] }
|
1039
|
+
] },
|
1031
1040
|
{ top_id: 2, middle_1: [
|
1032
|
-
{middle_1_id: 21}, {middle_1_id: 22, middle_2: [
|
1033
|
-
|
1041
|
+
{ middle_1_id: 21 }, { middle_1_id: 22, middle_2: [
|
1042
|
+
{ middle_2_id: 221 }
|
1043
|
+
] }
|
1044
|
+
] },
|
1034
1045
|
{ top_id: 3, middle_1: [
|
1035
|
-
{middle_1_id: 31}, {middle_1_id: 32}
|
1046
|
+
{ middle_1_id: 31 }, { middle_1_id: 32 }
|
1047
|
+
] },
|
1036
1048
|
{ top_id: 4 }
|
1037
1049
|
]
|
1038
1050
|
}
|
1039
1051
|
|
1040
1052
|
get '/multi_level', data
|
1041
|
-
expect(last_response.body).to eq(
|
1053
|
+
expect(last_response.body).to eq('multi_level works!')
|
1042
1054
|
expect(last_response.status).to eq(200)
|
1043
1055
|
end
|
1044
1056
|
|
1045
|
-
it
|
1057
|
+
it 'with invalid data' do
|
1046
1058
|
data = {
|
1047
1059
|
top: [
|
1048
1060
|
{ top_id: 1, middle_1: [
|
1049
|
-
{middle_1_id: 11}, {middle_1_id: 12, middle_2: [
|
1050
|
-
|
1061
|
+
{ middle_1_id: 11 }, { middle_1_id: 12, middle_2: [
|
1062
|
+
{ middle_2_id: 121 }, { middle_2_id: 122, bottom: [{ bottom_id: nil }] }
|
1063
|
+
] }
|
1064
|
+
] },
|
1051
1065
|
{ top_id: 2, middle_1: [
|
1052
|
-
{middle_1_id: 21}, {middle_1_id: 22, middle_2: [{middle_2_id: nil}]}
|
1066
|
+
{ middle_1_id: 21 }, { middle_1_id: 22, middle_2: [{ middle_2_id: nil }] }
|
1067
|
+
] },
|
1053
1068
|
{ top_id: 3, middle_1: [
|
1054
|
-
{middle_1_id: nil}, {middle_1_id: 32}
|
1069
|
+
{ middle_1_id: nil }, { middle_1_id: 32 }
|
1070
|
+
] },
|
1055
1071
|
{ top_id: nil, missing_top_id: 4 }
|
1056
1072
|
]
|
1057
1073
|
}
|
1058
1074
|
# debugger
|
1059
1075
|
get '/multi_level', data
|
1060
|
-
expect(last_response.body.split(
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1076
|
+
expect(last_response.body.split(', ')).to match_array([
|
1077
|
+
'top[3][top_id] is empty',
|
1078
|
+
'top[2][middle_1][0][middle_1_id] is empty',
|
1079
|
+
'top[1][middle_1][1][middle_2][0][middle_2_id] is empty',
|
1080
|
+
'top[0][middle_1][1][middle_2][1][bottom][0][bottom_id] is empty'
|
1081
|
+
])
|
1066
1082
|
expect(last_response.status).to eq(400)
|
1067
1083
|
end
|
1068
1084
|
end
|
1069
1085
|
end
|
1070
1086
|
|
1071
|
-
it
|
1087
|
+
it 'exactly_one_of' do
|
1072
1088
|
subject.params do
|
1073
1089
|
requires :orders, type: Array do
|
1074
1090
|
requires :id, type: Integer
|
@@ -1086,17 +1102,17 @@ describe Grape::Validations do
|
|
1086
1102
|
|
1087
1103
|
data = {
|
1088
1104
|
orders: [
|
1089
|
-
{ id: 77, drugs: {batch_no:
|
1105
|
+
{ id: 77, drugs: { batch_no: 'A1234567' } },
|
1090
1106
|
{ id: 70 }
|
1091
1107
|
]
|
1092
1108
|
}
|
1093
1109
|
|
1094
1110
|
get '/exactly_one_of', data
|
1095
|
-
expect(last_response.body).to eq(
|
1111
|
+
expect(last_response.body).to eq('exactly_one_of works!')
|
1096
1112
|
expect(last_response.status).to eq(200)
|
1097
1113
|
end
|
1098
1114
|
|
1099
|
-
it
|
1115
|
+
it 'at_least_one_of' do
|
1100
1116
|
subject.params do
|
1101
1117
|
requires :orders, type: Array do
|
1102
1118
|
requires :id, type: Integer
|
@@ -1114,17 +1130,17 @@ describe Grape::Validations do
|
|
1114
1130
|
|
1115
1131
|
data = {
|
1116
1132
|
orders: [
|
1117
|
-
{ id: 77, drugs: {batch_no:
|
1133
|
+
{ id: 77, drugs: { batch_no: 'A1234567' } },
|
1118
1134
|
{ id: 70 }
|
1119
1135
|
]
|
1120
1136
|
}
|
1121
1137
|
|
1122
1138
|
get '/at_least_one_of', data
|
1123
|
-
expect(last_response.body).to eq(
|
1139
|
+
expect(last_response.body).to eq('at_least_one_of works!')
|
1124
1140
|
expect(last_response.status).to eq(200)
|
1125
1141
|
end
|
1126
1142
|
|
1127
|
-
it
|
1143
|
+
it 'all_or_none_of' do
|
1128
1144
|
subject.params do
|
1129
1145
|
requires :orders, type: Array do
|
1130
1146
|
requires :id, type: Integer
|
@@ -1142,13 +1158,13 @@ describe Grape::Validations do
|
|
1142
1158
|
|
1143
1159
|
data = {
|
1144
1160
|
orders: [
|
1145
|
-
{ id: 77, drugs: {batch_no:
|
1161
|
+
{ id: 77, drugs: { batch_no: 'A1234567', batch_id: '12' } },
|
1146
1162
|
{ id: 70 }
|
1147
1163
|
]
|
1148
1164
|
}
|
1149
1165
|
|
1150
1166
|
get '/all_or_none_of', data
|
1151
|
-
expect(last_response.body).to eq(
|
1167
|
+
expect(last_response.body).to eq('all_or_none_of works!')
|
1152
1168
|
expect(last_response.status).to eq(200)
|
1153
1169
|
end
|
1154
1170
|
end
|
@@ -1173,15 +1189,24 @@ describe Grape::Validations do
|
|
1173
1189
|
end
|
1174
1190
|
|
1175
1191
|
context 'custom validation' do
|
1176
|
-
|
1177
|
-
|
1192
|
+
let(:custom_validator) do
|
1193
|
+
Class.new(Grape::Validations::Validators::Base) do
|
1178
1194
|
def validate_param!(attr_name, params)
|
1179
1195
|
return if params[attr_name] == 'im custom'
|
1196
|
+
|
1180
1197
|
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: 'is not custom!')
|
1181
1198
|
end
|
1182
1199
|
end
|
1183
1200
|
end
|
1184
1201
|
|
1202
|
+
before do
|
1203
|
+
described_class.register_validator('customvalidator', custom_validator)
|
1204
|
+
end
|
1205
|
+
|
1206
|
+
after do
|
1207
|
+
described_class.deregister_validator('customvalidator')
|
1208
|
+
end
|
1209
|
+
|
1185
1210
|
context 'when using optional with a custom validator' do
|
1186
1211
|
before do
|
1187
1212
|
subject.params do
|
@@ -1321,15 +1346,24 @@ describe Grape::Validations do
|
|
1321
1346
|
end
|
1322
1347
|
|
1323
1348
|
context 'when using options on param' do
|
1324
|
-
|
1325
|
-
|
1349
|
+
let(:custom_validator_with_options) do
|
1350
|
+
Class.new(Grape::Validations::Validators::Base) do
|
1326
1351
|
def validate_param!(attr_name, params)
|
1327
1352
|
return if params[attr_name] == @option[:text]
|
1353
|
+
|
1328
1354
|
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
|
1329
1355
|
end
|
1330
1356
|
end
|
1331
1357
|
end
|
1332
1358
|
|
1359
|
+
before do
|
1360
|
+
described_class.register_validator('customvalidator_with_options', custom_validator_with_options)
|
1361
|
+
end
|
1362
|
+
|
1363
|
+
after do
|
1364
|
+
described_class.deregister_validator('customvalidator_with_options')
|
1365
|
+
end
|
1366
|
+
|
1333
1367
|
before do
|
1334
1368
|
subject.params do
|
1335
1369
|
optional :custom, customvalidator_with_options: { text: 'im custom with options', message: 'is not custom with options!' }
|
@@ -1424,21 +1458,25 @@ describe Grape::Validations do
|
|
1424
1458
|
}
|
1425
1459
|
end
|
1426
1460
|
end
|
1461
|
+
|
1427
1462
|
it 'returns defaults' do
|
1428
1463
|
get '/order'
|
1429
1464
|
expect(last_response.status).to eq(200)
|
1430
1465
|
expect(last_response.body).to eq({ order: :asc, order_by: :created_at }.to_json)
|
1431
1466
|
end
|
1467
|
+
|
1432
1468
|
it 'overrides default value for order' do
|
1433
1469
|
get '/order?order=desc'
|
1434
1470
|
expect(last_response.status).to eq(200)
|
1435
1471
|
expect(last_response.body).to eq({ order: :desc, order_by: :created_at }.to_json)
|
1436
1472
|
end
|
1473
|
+
|
1437
1474
|
it 'overrides default value for order_by' do
|
1438
1475
|
get '/order?order_by=name'
|
1439
1476
|
expect(last_response.status).to eq(200)
|
1440
1477
|
expect(last_response.body).to eq({ order: :asc, order_by: :name }.to_json)
|
1441
1478
|
end
|
1479
|
+
|
1442
1480
|
it 'fails with invalid value' do
|
1443
1481
|
get '/order?order=invalid'
|
1444
1482
|
expect(last_response.status).to eq(400)
|
@@ -1463,7 +1501,7 @@ describe Grape::Validations do
|
|
1463
1501
|
|
1464
1502
|
context 'all or none' do
|
1465
1503
|
context 'optional params' do
|
1466
|
-
before
|
1504
|
+
before do
|
1467
1505
|
subject.resource :custom_message do
|
1468
1506
|
params do
|
1469
1507
|
optional :beer
|
@@ -1476,17 +1514,20 @@ describe Grape::Validations do
|
|
1476
1514
|
end
|
1477
1515
|
end
|
1478
1516
|
end
|
1517
|
+
|
1479
1518
|
context 'with a custom validation message' do
|
1480
1519
|
it 'errors when any one is present' do
|
1481
1520
|
get '/custom_message/all_or_none', beer: 'string'
|
1482
1521
|
expect(last_response.status).to eq(400)
|
1483
1522
|
expect(last_response.body).to eq 'beer, wine, juice all params are required or none is required'
|
1484
1523
|
end
|
1524
|
+
|
1485
1525
|
it 'works when all params are present' do
|
1486
1526
|
get '/custom_message/all_or_none', beer: 'string', wine: 'anotherstring', juice: 'anotheranotherstring'
|
1487
1527
|
expect(last_response.status).to eq(200)
|
1488
1528
|
expect(last_response.body).to eq 'all_or_none works!'
|
1489
1529
|
end
|
1530
|
+
|
1490
1531
|
it 'works when none are present' do
|
1491
1532
|
get '/custom_message/all_or_none'
|
1492
1533
|
expect(last_response.status).to eq(200)
|
@@ -1650,7 +1691,7 @@ describe Grape::Validations do
|
|
1650
1691
|
|
1651
1692
|
context 'exactly one of' do
|
1652
1693
|
context 'params' do
|
1653
|
-
before
|
1694
|
+
before do
|
1654
1695
|
subject.resources :custom_message do
|
1655
1696
|
params do
|
1656
1697
|
optional :beer
|
@@ -1714,7 +1755,7 @@ describe Grape::Validations do
|
|
1714
1755
|
end
|
1715
1756
|
|
1716
1757
|
context 'nested params' do
|
1717
|
-
before
|
1758
|
+
before do
|
1718
1759
|
subject.params do
|
1719
1760
|
requires :nested, type: Hash do
|
1720
1761
|
optional :beer_nested
|
@@ -1756,7 +1797,7 @@ describe Grape::Validations do
|
|
1756
1797
|
|
1757
1798
|
context 'at least one of' do
|
1758
1799
|
context 'params' do
|
1759
|
-
before
|
1800
|
+
before do
|
1760
1801
|
subject.resources :custom_message do
|
1761
1802
|
params do
|
1762
1803
|
optional :beer
|
@@ -1820,7 +1861,7 @@ describe Grape::Validations do
|
|
1820
1861
|
end
|
1821
1862
|
|
1822
1863
|
context 'nested params' do
|
1823
|
-
before
|
1864
|
+
before do
|
1824
1865
|
subject.params do
|
1825
1866
|
requires :nested, type: Hash do
|
1826
1867
|
optional :beer
|
@@ -6,10 +6,10 @@ require 'grape'
|
|
6
6
|
describe Grape do
|
7
7
|
it 'eager_load!' do
|
8
8
|
require 'grape/eager_load'
|
9
|
-
expect {
|
9
|
+
expect { described_class.eager_load! }.not_to raise_error
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'compile!' do
|
13
|
-
expect { Class.new(Grape::API).compile! }.
|
13
|
+
expect { Class.new(Grape::API).compile! }.not_to raise_error
|
14
14
|
end
|
15
15
|
end
|
@@ -35,14 +35,14 @@ shared_examples_for 'versioning' do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
versioned_get '/awesome', 'v1', **macro_options
|
38
|
-
expect(last_response.status).to
|
38
|
+
expect(last_response.status).to be 404
|
39
39
|
|
40
40
|
versioned_get '/awesome', 'v2', **macro_options
|
41
|
-
expect(last_response.status).to
|
41
|
+
expect(last_response.status).to be 200
|
42
42
|
versioned_get '/legacy', 'v1', **macro_options
|
43
|
-
expect(last_response.status).to
|
43
|
+
expect(last_response.status).to be 200
|
44
44
|
versioned_get '/legacy', 'v2', **macro_options
|
45
|
-
expect(last_response.status).to
|
45
|
+
expect(last_response.status).to be 404
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'is able to specify multiple versions' do
|
@@ -52,11 +52,11 @@ shared_examples_for 'versioning' do
|
|
52
52
|
end
|
53
53
|
|
54
54
|
versioned_get '/awesome', 'v1', **macro_options
|
55
|
-
expect(last_response.status).to
|
55
|
+
expect(last_response.status).to be 200
|
56
56
|
versioned_get '/awesome', 'v2', **macro_options
|
57
|
-
expect(last_response.status).to
|
57
|
+
expect(last_response.status).to be 200
|
58
58
|
versioned_get '/awesome', 'v3', **macro_options
|
59
|
-
expect(last_response.status).to
|
59
|
+
expect(last_response.status).to be 404
|
60
60
|
end
|
61
61
|
|
62
62
|
context 'with different versions for the same endpoint' do
|
@@ -70,7 +70,7 @@ shared_examples_for 'versioning' do
|
|
70
70
|
|
71
71
|
subject.version 'v1', macro_options do
|
72
72
|
get 'version' do
|
73
|
-
|
73
|
+
"version #{request.env['api.version']}"
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
@@ -94,7 +94,7 @@ shared_examples_for 'versioning' do
|
|
94
94
|
|
95
95
|
subject.version 'v1', macro_options do
|
96
96
|
get 'version' do
|
97
|
-
|
97
|
+
"version #{request.env['api.version']}"
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
@@ -117,6 +117,7 @@ shared_examples_for 'versioning' do
|
|
117
117
|
before do
|
118
118
|
@output ||= 'v2-'
|
119
119
|
end
|
120
|
+
|
120
121
|
get 'version' do
|
121
122
|
@output += 'version'
|
122
123
|
end
|
@@ -126,6 +127,7 @@ shared_examples_for 'versioning' do
|
|
126
127
|
before do
|
127
128
|
@output ||= 'v1-'
|
128
129
|
end
|
130
|
+
|
129
131
|
get 'version' do
|
130
132
|
@output += 'version'
|
131
133
|
end
|
@@ -170,6 +172,7 @@ shared_examples_for 'versioning' do
|
|
170
172
|
end
|
171
173
|
klass
|
172
174
|
end
|
175
|
+
|
173
176
|
before do
|
174
177
|
subject.format :txt
|
175
178
|
|
data/spec/spec_helper.rb
CHANGED
@@ -5,12 +5,21 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
5
5
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'support'))
|
6
6
|
|
7
7
|
require 'grape'
|
8
|
+
require 'test_prof/recipes/rspec/let_it_be'
|
9
|
+
|
10
|
+
class NullAdapter
|
11
|
+
def begin_transaction; end
|
12
|
+
|
13
|
+
def rollback_transaction; end
|
14
|
+
end
|
15
|
+
|
16
|
+
TestProf::BeforeAll.adapter = NullAdapter.new
|
8
17
|
|
9
18
|
require 'rubygems'
|
10
19
|
require 'bundler'
|
11
20
|
Bundler.require :default, :test
|
12
21
|
|
13
|
-
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each do |file|
|
22
|
+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].sort.each do |file|
|
14
23
|
require file
|
15
24
|
end
|
16
25
|
|
@@ -27,7 +36,8 @@ RSpec.configure do |config|
|
|
27
36
|
config.filter_run_when_matching :focus
|
28
37
|
config.warnings = true
|
29
38
|
|
30
|
-
config.before(:
|
39
|
+
config.before(:all) { Grape::Util::InheritableSetting.reset_global! }
|
40
|
+
config.before { Grape::Util::InheritableSetting.reset_global! }
|
31
41
|
|
32
42
|
# Enable flags like --only-failures and --next-failure
|
33
43
|
config.example_status_persistence_file_path = '.rspec_status'
|