jsonapi-resources 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/jsonapi-resources.rb +2 -1
- data/lib/jsonapi/configuration.rb +1 -1
- data/lib/jsonapi/error_codes.rb +1 -0
- data/lib/jsonapi/exceptions.rb +14 -0
- data/lib/jsonapi/mime_types.rb +9 -0
- data/lib/jsonapi/resource.rb +1 -1
- data/lib/jsonapi/resource_controller.rb +18 -0
- data/lib/jsonapi/resources/version.rb +1 -1
- data/test/controllers/controller_test.rb +50 -0
- data/test/integration/requests/request_test.rb +100 -1
- data/test/test_helper.rb +1 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3cc147ec7ed0dc456d04b86c715a7e9a13f47dd
|
4
|
+
data.tar.gz: 77d151a254fa986e93ba80fd06775ed0bd3753c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3349452f04356593d52125551ed57049a91f2b89a66010587d562c12d1fd47ffab5896b8078434bd733a7a931c772478d8615afa30240e66b60b9a9f10ae14f
|
7
|
+
data.tar.gz: 4c75390e027e8af857d049f883836af04e6258ccd57209de458e0d63b809039684febc28720d3a3973e2a65c2068b70775f439b575e33a74b108a7da738bcc9c
|
data/lib/jsonapi-resources.rb
CHANGED
data/lib/jsonapi/error_codes.rb
CHANGED
data/lib/jsonapi/exceptions.rb
CHANGED
@@ -30,6 +30,20 @@ module JSONAPI
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
+
class UnsupportedMediaTypeError < Error
|
34
|
+
attr_accessor :media_type
|
35
|
+
def initialize(media_type)
|
36
|
+
@media_type = media_type
|
37
|
+
end
|
38
|
+
|
39
|
+
def errors
|
40
|
+
[JSONAPI::Error.new(code: JSONAPI::UNSUPPORTED_MEDIA_TYPE,
|
41
|
+
status: :unsupported_media_type,
|
42
|
+
title: 'Unsupported media type',
|
43
|
+
detail: "All requests that create or update resources must use the '#{JSONAPI::MEDIA_TYPE}' Content-Type. This request specified '#{media_type}.'")]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
33
47
|
class HasManyRelationExists < Error
|
34
48
|
attr_accessor :id
|
35
49
|
def initialize(id)
|
data/lib/jsonapi/resource.rb
CHANGED
@@ -117,7 +117,7 @@ module JSONAPI
|
|
117
117
|
def _save
|
118
118
|
@model.save!
|
119
119
|
@save_needed = false
|
120
|
-
rescue ActiveRecord::RecordInvalid => e
|
120
|
+
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
|
121
121
|
raise JSONAPI::Exceptions::ValidationErrors.new(e.record.errors.messages)
|
122
122
|
end
|
123
123
|
|
@@ -13,7 +13,9 @@ module JSONAPI
|
|
13
13
|
class ResourceController < ActionController::Base
|
14
14
|
include ResourceFor
|
15
15
|
|
16
|
+
before_filter :ensure_correct_media_type, only: [:create, :update, :create_association, :update_association]
|
16
17
|
before_filter :setup_request
|
18
|
+
after_filter :setup_response
|
17
19
|
|
18
20
|
def index
|
19
21
|
render json: JSONAPI::ResourceSerializer.new(resource_klass).serialize_to_hash(
|
@@ -107,6 +109,16 @@ module JSONAPI
|
|
107
109
|
@resource_klass_name ||= "#{self.class.name.sub(/Controller$/, '').singularize}Resource"
|
108
110
|
end
|
109
111
|
|
112
|
+
def ensure_correct_media_type
|
113
|
+
unless request.content_type == JSONAPI::MEDIA_TYPE
|
114
|
+
raise JSONAPI::Exceptions::UnsupportedMediaTypeError.new(request.content_type)
|
115
|
+
end
|
116
|
+
rescue => e
|
117
|
+
# :nocov:
|
118
|
+
handle_exceptions(e)
|
119
|
+
# :nocov:
|
120
|
+
end
|
121
|
+
|
110
122
|
def setup_request
|
111
123
|
@request = JSONAPI::Request.new(params, {
|
112
124
|
context: context,
|
@@ -119,6 +131,12 @@ module JSONAPI
|
|
119
131
|
# :nocov:
|
120
132
|
end
|
121
133
|
|
134
|
+
def setup_response
|
135
|
+
if response.body.size > 0
|
136
|
+
response.headers['Content-Type'] = JSONAPI::MEDIA_TYPE
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
122
140
|
def parse_key_array(raw)
|
123
141
|
keys = raw.nil? || raw.empty? ? [] : raw.split(',')
|
124
142
|
resource_klass.verify_keys(keys, context)
|
@@ -1,6 +1,10 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
require File.expand_path('../../fixtures/active_record', __FILE__)
|
3
3
|
|
4
|
+
def set_content_type_header!
|
5
|
+
@request.headers['Content-Type'] = JSONAPI::MEDIA_TYPE
|
6
|
+
end
|
7
|
+
|
4
8
|
class PostsControllerTest < ActionController::TestCase
|
5
9
|
def test_index
|
6
10
|
get :index
|
@@ -262,6 +266,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
262
266
|
end
|
263
267
|
|
264
268
|
def test_create_simple
|
269
|
+
set_content_type_header!
|
265
270
|
post :create,
|
266
271
|
{
|
267
272
|
posts: {
|
@@ -281,6 +286,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
281
286
|
end
|
282
287
|
|
283
288
|
def test_create_link_to_missing_object
|
289
|
+
set_content_type_header!
|
284
290
|
post :create,
|
285
291
|
{
|
286
292
|
posts: {
|
@@ -298,6 +304,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
298
304
|
end
|
299
305
|
|
300
306
|
def test_create_extra_param
|
307
|
+
set_content_type_header!
|
301
308
|
post :create,
|
302
309
|
{
|
303
310
|
posts: {
|
@@ -315,6 +322,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
315
322
|
end
|
316
323
|
|
317
324
|
def test_create_with_invalid_data
|
325
|
+
set_content_type_header!
|
318
326
|
post :create,
|
319
327
|
{
|
320
328
|
posts: {
|
@@ -338,6 +346,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
338
346
|
end
|
339
347
|
|
340
348
|
def test_create_multiple
|
349
|
+
set_content_type_header!
|
341
350
|
post :create,
|
342
351
|
{
|
343
352
|
posts: [
|
@@ -367,6 +376,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
367
376
|
end
|
368
377
|
|
369
378
|
def test_create_multiple_wrong_case
|
379
|
+
set_content_type_header!
|
370
380
|
post :create,
|
371
381
|
{
|
372
382
|
posts: [
|
@@ -392,6 +402,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
392
402
|
end
|
393
403
|
|
394
404
|
def test_create_simple_missing_posts
|
405
|
+
set_content_type_header!
|
395
406
|
post :create,
|
396
407
|
{
|
397
408
|
posts_spelled_wrong: {
|
@@ -408,6 +419,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
408
419
|
end
|
409
420
|
|
410
421
|
def test_create_simple_unpermitted_attributes
|
422
|
+
set_content_type_header!
|
411
423
|
post :create,
|
412
424
|
{
|
413
425
|
posts: {
|
@@ -424,6 +436,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
424
436
|
end
|
425
437
|
|
426
438
|
def test_create_with_links
|
439
|
+
set_content_type_header!
|
427
440
|
post :create,
|
428
441
|
{
|
429
442
|
posts: {
|
@@ -445,6 +458,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
445
458
|
end
|
446
459
|
|
447
460
|
def test_create_with_links_include_and_fields
|
461
|
+
set_content_type_header!
|
448
462
|
post :create,
|
449
463
|
{
|
450
464
|
posts: {
|
@@ -471,6 +485,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
471
485
|
end
|
472
486
|
|
473
487
|
def test_update_with_links
|
488
|
+
set_content_type_header!
|
474
489
|
javascript = Section.find_by(name: 'javascript')
|
475
490
|
|
476
491
|
put :update,
|
@@ -495,6 +510,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
495
510
|
end
|
496
511
|
|
497
512
|
def test_update_remove_links
|
513
|
+
set_content_type_header!
|
498
514
|
put :update,
|
499
515
|
{
|
500
516
|
id: 3,
|
@@ -517,6 +533,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
517
533
|
end
|
518
534
|
|
519
535
|
def test_update_relationship_has_one
|
536
|
+
set_content_type_header!
|
520
537
|
ruby = Section.find_by(name: 'ruby')
|
521
538
|
post_object = Post.find(3)
|
522
539
|
assert_not_equal ruby.id, post_object.section_id
|
@@ -529,6 +546,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
529
546
|
end
|
530
547
|
|
531
548
|
def test_update_relationship_has_one_singular_param
|
549
|
+
set_content_type_header!
|
532
550
|
ruby = Section.find_by(name: 'ruby')
|
533
551
|
post_object = Post.find(3)
|
534
552
|
|
@@ -538,6 +556,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
538
556
|
end
|
539
557
|
|
540
558
|
def test_update_relationship_has_one_singular_param_relation_nil
|
559
|
+
set_content_type_header!
|
541
560
|
ruby = Section.find_by(name: 'ruby')
|
542
561
|
post_object = Post.find(3)
|
543
562
|
post_object.section_id = nil
|
@@ -551,6 +570,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
551
570
|
end
|
552
571
|
|
553
572
|
def test_create_relationship_has_one_singular_param_relation_nil
|
573
|
+
set_content_type_header!
|
554
574
|
ruby = Section.find_by(name: 'ruby')
|
555
575
|
post_object = Post.find(3)
|
556
576
|
post_object.section_id = nil
|
@@ -564,6 +584,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
564
584
|
end
|
565
585
|
|
566
586
|
def test_create_relationship_has_one_singular_param_relation_not_nil
|
587
|
+
set_content_type_header!
|
567
588
|
ruby = Section.find_by(name: 'ruby')
|
568
589
|
js = Section.find_by(name: 'javascript')
|
569
590
|
post_object = Post.find(3)
|
@@ -579,6 +600,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
579
600
|
end
|
580
601
|
|
581
602
|
def test_update_relationship_has_many_join_table_single
|
603
|
+
set_content_type_header!
|
582
604
|
put :update_association, {post_id: 3, association: 'tags', tags: []}
|
583
605
|
assert_response :no_content
|
584
606
|
|
@@ -601,6 +623,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
601
623
|
end
|
602
624
|
|
603
625
|
def test_update_relationship_has_many_join_table
|
626
|
+
set_content_type_header!
|
604
627
|
put :update_association, {post_id: 3, association: 'tags', tags: [2, 3]}
|
605
628
|
|
606
629
|
assert_response :no_content
|
@@ -610,6 +633,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
610
633
|
end
|
611
634
|
|
612
635
|
def test_create_relationship_has_many_join_table
|
636
|
+
set_content_type_header!
|
613
637
|
put :update_association, {post_id: 3, association: 'tags', tags: [2, 3]}
|
614
638
|
|
615
639
|
assert_response :no_content
|
@@ -626,6 +650,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
626
650
|
end
|
627
651
|
|
628
652
|
def test_create_relationship_has_many_missing_tags
|
653
|
+
set_content_type_header!
|
629
654
|
post :create_association, {post_id: 3, association: 'tags'}
|
630
655
|
|
631
656
|
assert_response :bad_request
|
@@ -633,6 +658,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
633
658
|
end
|
634
659
|
|
635
660
|
def test_create_relationship_has_many_join_table_record_exists
|
661
|
+
set_content_type_header!
|
636
662
|
put :update_association, {post_id: 3, association: 'tags', tags: [2, 3]}
|
637
663
|
|
638
664
|
assert_response :no_content
|
@@ -647,6 +673,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
647
673
|
end
|
648
674
|
|
649
675
|
def test_update_relationship_has_one_mismatch_params
|
676
|
+
set_content_type_header!
|
650
677
|
post :create_association, {post_id: 3, association: 'section', authors: 1}
|
651
678
|
|
652
679
|
assert_response :bad_request
|
@@ -654,6 +681,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
654
681
|
end
|
655
682
|
|
656
683
|
def test_update_relationship_has_many_missing_tags
|
684
|
+
set_content_type_header!
|
657
685
|
put :update_association, {post_id: 3, association: 'tags'}
|
658
686
|
|
659
687
|
assert_response :bad_request
|
@@ -661,6 +689,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
661
689
|
end
|
662
690
|
|
663
691
|
def test_delete_relationship_has_one
|
692
|
+
set_content_type_header!
|
664
693
|
ruby = Section.find_by(name: 'ruby')
|
665
694
|
|
666
695
|
post :create_association, {post_id: 9, association: 'section', sections: ruby.id}
|
@@ -675,6 +704,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
675
704
|
end
|
676
705
|
|
677
706
|
def test_delete_relationship_has_many
|
707
|
+
set_content_type_header!
|
678
708
|
put :update_association, {post_id: 9, association: 'tags', tags: [2, 3]}
|
679
709
|
assert_response :no_content
|
680
710
|
p = Post.find(9)
|
@@ -688,6 +718,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
688
718
|
end
|
689
719
|
|
690
720
|
def test_delete_relationship_has_many_does_not_exist
|
721
|
+
set_content_type_header!
|
691
722
|
put :update_association, {post_id: 9, association: 'tags', tags: [2, 3]}
|
692
723
|
assert_response :no_content
|
693
724
|
p = Post.find(9)
|
@@ -701,6 +732,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
701
732
|
end
|
702
733
|
|
703
734
|
def test_update_mismatched_keys
|
735
|
+
set_content_type_header!
|
704
736
|
javascript = Section.find_by(name: 'javascript')
|
705
737
|
|
706
738
|
put :update,
|
@@ -721,6 +753,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
721
753
|
end
|
722
754
|
|
723
755
|
def test_update_extra_param
|
756
|
+
set_content_type_header!
|
724
757
|
javascript = Section.find_by(name: 'javascript')
|
725
758
|
|
726
759
|
put :update,
|
@@ -741,6 +774,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
741
774
|
end
|
742
775
|
|
743
776
|
def test_update_extra_param_in_links
|
777
|
+
set_content_type_header!
|
744
778
|
javascript = Section.find_by(name: 'javascript')
|
745
779
|
|
746
780
|
put :update,
|
@@ -761,6 +795,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
761
795
|
end
|
762
796
|
|
763
797
|
def test_update_missing_param
|
798
|
+
set_content_type_header!
|
764
799
|
javascript = Section.find_by(name: 'javascript')
|
765
800
|
|
766
801
|
put :update,
|
@@ -780,6 +815,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
780
815
|
end
|
781
816
|
|
782
817
|
def test_update_multiple
|
818
|
+
set_content_type_header!
|
783
819
|
javascript = Section.find_by(name: 'javascript')
|
784
820
|
|
785
821
|
put :update,
|
@@ -820,6 +856,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
820
856
|
end
|
821
857
|
|
822
858
|
def test_update_multiple_missing_keys
|
859
|
+
set_content_type_header!
|
823
860
|
javascript = Section.find_by(name: 'javascript')
|
824
861
|
|
825
862
|
put :update,
|
@@ -847,6 +884,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
847
884
|
end
|
848
885
|
|
849
886
|
def test_update_mismatch_keys
|
887
|
+
set_content_type_header!
|
850
888
|
javascript = Section.find_by(name: 'javascript')
|
851
889
|
|
852
890
|
put :update,
|
@@ -876,6 +914,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
876
914
|
end
|
877
915
|
|
878
916
|
def test_update_multiple_count_mismatch
|
917
|
+
set_content_type_header!
|
879
918
|
javascript = Section.find_by(name: 'javascript')
|
880
919
|
|
881
920
|
put :update,
|
@@ -905,6 +944,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
905
944
|
end
|
906
945
|
|
907
946
|
def test_update_unpermitted_attributes
|
947
|
+
set_content_type_header!
|
908
948
|
put :update,
|
909
949
|
{
|
910
950
|
id: 3,
|
@@ -923,6 +963,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
923
963
|
end
|
924
964
|
|
925
965
|
def test_update_bad_attributes
|
966
|
+
set_content_type_header!
|
926
967
|
put :update,
|
927
968
|
{
|
928
969
|
id: 3,
|
@@ -1086,6 +1127,7 @@ class ExpenseEntriesControllerTest < ActionController::TestCase
|
|
1086
1127
|
end
|
1087
1128
|
|
1088
1129
|
def test_create_expense_entries_underscored
|
1130
|
+
set_content_type_header!
|
1089
1131
|
JSONAPI.configuration.json_key_format = :underscored_key
|
1090
1132
|
|
1091
1133
|
post :create,
|
@@ -1113,6 +1155,7 @@ class ExpenseEntriesControllerTest < ActionController::TestCase
|
|
1113
1155
|
end
|
1114
1156
|
|
1115
1157
|
def test_create_expense_entries_camelized_key
|
1158
|
+
set_content_type_header!
|
1116
1159
|
JSONAPI.configuration.json_key_format = :camelized_key
|
1117
1160
|
|
1118
1161
|
post :create,
|
@@ -1140,6 +1183,7 @@ class ExpenseEntriesControllerTest < ActionController::TestCase
|
|
1140
1183
|
end
|
1141
1184
|
|
1142
1185
|
def test_create_expense_entries_dasherized_key
|
1186
|
+
set_content_type_header!
|
1143
1187
|
JSONAPI.configuration.json_key_format = :dasherized_key
|
1144
1188
|
|
1145
1189
|
post :create,
|
@@ -1287,6 +1331,7 @@ end
|
|
1287
1331
|
|
1288
1332
|
class PeopleControllerTest < ActionController::TestCase
|
1289
1333
|
def test_create_validations
|
1334
|
+
set_content_type_header!
|
1290
1335
|
post :create,
|
1291
1336
|
{
|
1292
1337
|
people: {
|
@@ -1300,6 +1345,7 @@ class PeopleControllerTest < ActionController::TestCase
|
|
1300
1345
|
end
|
1301
1346
|
|
1302
1347
|
def test_create_validations_missing_attribute
|
1348
|
+
set_content_type_header!
|
1303
1349
|
post :create,
|
1304
1350
|
{
|
1305
1351
|
people: {
|
@@ -1316,6 +1362,7 @@ class PeopleControllerTest < ActionController::TestCase
|
|
1316
1362
|
end
|
1317
1363
|
|
1318
1364
|
def test_update_validations_missing_attribute
|
1365
|
+
set_content_type_header!
|
1319
1366
|
put :update,
|
1320
1367
|
{
|
1321
1368
|
id: 3,
|
@@ -1404,6 +1451,7 @@ class BreedsControllerTest < ActionController::TestCase
|
|
1404
1451
|
end
|
1405
1452
|
|
1406
1453
|
def test_poro_create_simple
|
1454
|
+
set_content_type_header!
|
1407
1455
|
post :create,
|
1408
1456
|
{
|
1409
1457
|
breeds: {
|
@@ -1417,6 +1465,7 @@ class BreedsControllerTest < ActionController::TestCase
|
|
1417
1465
|
end
|
1418
1466
|
|
1419
1467
|
def test_poro_create_update
|
1468
|
+
set_content_type_header!
|
1420
1469
|
post :create,
|
1421
1470
|
{
|
1422
1471
|
breeds: {
|
@@ -1500,6 +1549,7 @@ class Api::V1::PostsControllerTest < ActionController::TestCase
|
|
1500
1549
|
end
|
1501
1550
|
|
1502
1551
|
def test_create_simple_namespaced
|
1552
|
+
set_content_type_header!
|
1503
1553
|
post :create,
|
1504
1554
|
{
|
1505
1555
|
posts: {
|
@@ -45,6 +45,21 @@ class RequestTest < ActionDispatch::IntegrationTest
|
|
45
45
|
assert_equal 'USD', json_response['isoCurrency']
|
46
46
|
end
|
47
47
|
|
48
|
+
def test_put_single_without_content_type
|
49
|
+
put '/posts/3',
|
50
|
+
{
|
51
|
+
'posts' => {
|
52
|
+
'id' => '3',
|
53
|
+
'title' => 'A great new Post',
|
54
|
+
'links' => {
|
55
|
+
'tags' => [3, 4]
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}.to_json, "CONTENT_TYPE" => "application/json"
|
59
|
+
|
60
|
+
assert_equal 415, status
|
61
|
+
end
|
62
|
+
|
48
63
|
def test_put_single
|
49
64
|
put '/posts/3',
|
50
65
|
{
|
@@ -55,13 +70,97 @@ class RequestTest < ActionDispatch::IntegrationTest
|
|
55
70
|
'tags' => [3, 4]
|
56
71
|
}
|
57
72
|
}
|
58
|
-
}
|
73
|
+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
|
74
|
+
|
59
75
|
assert_equal 200, status
|
60
76
|
end
|
61
77
|
|
78
|
+
def test_post_single_without_content_type
|
79
|
+
post '/posts',
|
80
|
+
{
|
81
|
+
'posts' => {
|
82
|
+
'title' => 'A great new Post',
|
83
|
+
'links' => {
|
84
|
+
'tags' => [3, 4]
|
85
|
+
}
|
86
|
+
}
|
87
|
+
}.to_json, "CONTENT_TYPE" => "application/json"
|
88
|
+
|
89
|
+
assert_equal 415, status
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_post_single
|
93
|
+
post '/posts',
|
94
|
+
{
|
95
|
+
'posts' => {
|
96
|
+
'title' => 'A great new Post',
|
97
|
+
'body' => 'JSONAPIResources is the greatest thing since unsliced bread.',
|
98
|
+
'links' => {
|
99
|
+
'author' => '3'
|
100
|
+
}
|
101
|
+
}
|
102
|
+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
|
103
|
+
|
104
|
+
assert_equal 201, status
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_create_association_without_content_type
|
108
|
+
ruby = Section.find_by(name: 'ruby')
|
109
|
+
put '/posts/3/links/section', { 'sections' => ruby.id.to_s }.to_json
|
110
|
+
|
111
|
+
assert_equal 415, status
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_create_association
|
115
|
+
ruby = Section.find_by(name: 'ruby')
|
116
|
+
put '/posts/3/links/section', { 'sections' => ruby.id.to_s }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
|
117
|
+
|
118
|
+
assert_equal 204, status
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_index_content_type
|
122
|
+
get '/posts'
|
123
|
+
assert_match JSONAPI::MEDIA_TYPE, headers['Content-Type']
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_get_content_type
|
127
|
+
get '/posts/3'
|
128
|
+
assert_match JSONAPI::MEDIA_TYPE, headers['Content-Type']
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_put_content_type
|
132
|
+
put '/posts/3',
|
133
|
+
{
|
134
|
+
'posts' => {
|
135
|
+
'id' => '3',
|
136
|
+
'title' => 'A great new Post',
|
137
|
+
'links' => {
|
138
|
+
'tags' => [3, 4]
|
139
|
+
}
|
140
|
+
}
|
141
|
+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
|
142
|
+
|
143
|
+
assert_match JSONAPI::MEDIA_TYPE, headers['Content-Type']
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_post_correct_content_type
|
147
|
+
post '/posts',
|
148
|
+
{
|
149
|
+
'posts' => {
|
150
|
+
'title' => 'A great new Post',
|
151
|
+
'links' => {
|
152
|
+
'author' => '3'
|
153
|
+
}
|
154
|
+
}
|
155
|
+
}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
|
156
|
+
|
157
|
+
assert_match JSONAPI::MEDIA_TYPE, headers['Content-Type']
|
158
|
+
end
|
159
|
+
|
62
160
|
def test_destroy_single
|
63
161
|
delete '/posts/7'
|
64
162
|
assert_equal 204, status
|
163
|
+
assert_nil headers['Content-Type']
|
65
164
|
end
|
66
165
|
|
67
166
|
def test_destroy_multiple
|
data/test/test_helper.rb
CHANGED
@@ -14,6 +14,7 @@ require 'rails/all'
|
|
14
14
|
require 'jsonapi/routing_ext'
|
15
15
|
require 'jsonapi/configuration'
|
16
16
|
require 'jsonapi/formatter'
|
17
|
+
require 'jsonapi/mime_types'
|
17
18
|
|
18
19
|
require File.expand_path('../helpers/value_matchers', __FILE__)
|
19
20
|
require File.expand_path('../helpers/hash_helpers', __FILE__)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-resources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Gebhardt
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-01-
|
12
|
+
date: 2015-01-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -134,6 +134,7 @@ files:
|
|
134
134
|
- lib/jsonapi/error_codes.rb
|
135
135
|
- lib/jsonapi/exceptions.rb
|
136
136
|
- lib/jsonapi/formatter.rb
|
137
|
+
- lib/jsonapi/mime_types.rb
|
137
138
|
- lib/jsonapi/operation.rb
|
138
139
|
- lib/jsonapi/operation_result.rb
|
139
140
|
- lib/jsonapi/operations_processor.rb
|
@@ -177,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
177
178
|
version: '0'
|
178
179
|
requirements: []
|
179
180
|
rubyforge_project:
|
180
|
-
rubygems_version: 2.
|
181
|
+
rubygems_version: 2.4.5
|
181
182
|
signing_key:
|
182
183
|
specification_version: 4
|
183
184
|
summary: Easily support JSON API in Rails.
|