-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathresponse_spec.rb
More file actions
135 lines (119 loc) · 3.43 KB
/
response_spec.rb
File metadata and controls
135 lines (119 loc) · 3.43 KB
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
require 'spec_helper'
describe JSONAPI::Parser, '.parse_response!' do
it 'succeeds on nil data' do
payload = { 'data' => nil }
expect { JSONAPI.parse_response!(payload) }.not_to raise_error
end
it 'succeeds on empty array data' do
payload = { 'data' => [] }
expect { JSONAPI.parse_response!(payload) }.not_to raise_error
end
it 'works' do
payload = {
'data' => [
{
'type' => 'articles',
'id' => '1',
'attributes' => { 'title' => 'JSON API paints my bikeshed!' },
'links' => { 'self' => 'http://example.com/articles/1' },
'relationships' => {
'author' => {
'links' => {
'self' => 'http://example.com/articles/1/relationships/author',
'related' => 'http://example.com/articles/1/author'
},
'data' => { 'type' => 'people', 'id' => '9' }
},
'journal' => {
'data' => nil
},
'comments' => {
'links' => {
'self' => 'http://example.com/articles/1/relationships/comments',
'related' => 'http://example.com/articles/1/comments'
},
'data' => [
{ 'type' => 'comments', 'id' => '5' },
{ 'type' => 'comments', 'id' => '12' }
]
}
}
}
],
'meta' => { 'count' => '13' }
}
expect { JSONAPI.parse_response!(payload) }.not_to raise_error
end
it 'passes regardless of id/type order' do
payload = {
'data' => [
{
'type' => 'articles',
'id' => '1',
'relationships' => {
'comments' => {
'data' => [
{ 'type' => 'comments', 'id' => '5' },
{ 'id' => '12', 'type' => 'comments' }
]
}
}
}
]
}
expect { JSONAPI.parse_response!(payload) }.to_not raise_error
end
it 'fails when a top-level data array resource object is missing id' do
payload = {
'data' => [
{
'type' => 'articles',
'attributes' => {'title' => 'JSON API paints my bikeshed!'}
}
]
}
expect { JSONAPI.parse_response!(payload) }.to raise_error(
JSONAPI::Parser::InvalidDocument,
'A resource object must have an id.'
)
end
it 'fails when a relationship object is missing id' do
payload = {
'data' => [
{
'type' => 'articles',
'id' => '1',
'relationships' => {
'author' => {
'data' => { 'type' => 'people' }
}
}
}
]
}
expect { JSONAPI.parse_response!(payload) }.to raise_error(
JSONAPI::Parser::InvalidDocument,
'A resource identifier object MUST contain ["id", "type"] members.'
)
end
it 'fails when the top-level resource object has no type' do
payload = {
'data' => {
'id' => '1'
}
}
expect { JSONAPI.parse_response!(payload) }.to raise_error(
JSONAPI::Parser::InvalidDocument,
'A resource object must have a type.'
)
end
it 'passes when the top-level resource object has no id' do
payload = {
'data' => {
'type' => 'articles',
'attributes' => {'title' => 'JSON API paints my bikeshed!'}
}
}
expect { JSONAPI.parse_response!(payload) }.to_not raise_error
end
end