sr / saloon

AtomPub server implemented using Sinatra and CouchDB

This URL has Read+Write access

Simon Rozet (author)
Sun Nov 16 09:10:07 -0800 2008
commit  9ccc9d19882107c381c18344f0b50f799e82801d
tree    c69851f3abe82ae5f229861115b6b510f3e56e27
parent  3304a3f5a069f40b9e0cadd02479f8486ab18ab4
saloon / test / app_test.rb
100644 248 lines (201 sloc) 5.945 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
require File.dirname(__FILE__) + '/test_helper'
require File.dirname(__FILE__) + '/../lib/app'
 
describe 'App' do
  setup do
    @store = mock('store')
    Store.stubs(:new).returns(@store)
  end
 
  describe 'GET /' do
    def do_get
      get_it '/'
    end
 
    setup do
      @service = stub('an Atom::Service', :to_s => 'xml')
      @store.stubs(:service_document).returns(@service)
    end
 
    it 'is successful' do
      do_get
      should.be.ok
    end
 
    it 'is application/atomsvc+xml' do
      do_get
      headers['Content-Type'].should.equal 'application/atomsvc+xml'
    end
 
    it 'gets the service document' do
      @store.expects(:service_document).returns(@service)
      do_get
    end
 
    it 'returns the atom representation of the service document' do
      @service.expects(:to_s).returns('some xml representing the service doc')
      do_get
      body.should.equal 'some xml representing the service doc'
    end
  end
 
  describe 'GET /:collection' do
    def do_get
      get_it '/articles'
    end
 
    setup do
      @collection = stub('an Atom::Collection', :to_s => 'collection')
      @store.stubs(:find_collection).returns(@collection)
    end
 
    it 'is successful' do
      do_get
      should.be.ok
    end
 
    it 'is application/atom+xml' do
      do_get
      headers['Content-Type'].should.equal 'application/atom+xml'
    end
 
    it 'finds the given collection' do
      @store.expects(:find_collection).with('articles').returns(@collection)
      do_get
    end
 
    it 'returns the atom representation of the collection' do
      do_get
      body.should.equal @collection.to_s
    end
 
    describe 'When the collection is not found' do
      setup do
        @store.stubs(:find_collection).raises(CollectionNotFound)
      end
 
      it 'is not found' do
        get_it '/articles'
        should.be.not_found
      end
    end
  end
 
  describe 'POST /:collection' do
    def do_post
      post_it '/articles', @entry.to_s, :content_type => 'application/atom+xml;type=entry'
    end
 
    setup do
      @entry = Atom::Entry.new(:title => 'foo', :content => 'bar',
        :edit_url => 'http://example.org/my_collection/my_entry')
      @store.stubs(:create_entry).returns(@entry)
    end
 
    it 'is created' do
      do_post
      status.should.equal 201
    end
 
    it 'creates the entry' do
      @store.expects(:create_entry).with('articles', @entry.to_s)
      do_post
    end
 
    it 'is application/atom+xml;type=entry' do
      do_post
      headers['Content-Type'].should.equal 'application/atom+xml;type=entry'
    end
 
    it "sets the Location header to the entry's edit url" do
      do_post
      headers['Location'].should.equal 'http://example.org/my_collection/my_entry'
    end
 
    it 'returns the atom entry' do
      do_post
      body.should.equal @entry.to_s
    end
 
    describe 'When the collection is not found' do
      setup do
        @store.stubs(:create_entry).raises(CollectionNotFound)
      end
 
      it 'is not found' do
        post_it '/articles', @entry.to_s, :content_type => 'application/atom+xml;type=entry'
        should.be.not_found
      end
    end
  end
 
  describe 'GET /:collection/:entry' do
    def do_get
      get_it '/articles/my_entry'
    end
 
    setup do
      @entry = stub('an Atom::Entry', :to_s => 'hi, i am an atom entry')
      @store.stubs(:find_entry).returns(@entry)
    end
 
    it 'is successful' do
      do_get
      should.be.ok
    end
 
    it 'is application/atom+xml;type=entry' do
      do_get
      headers['Content-Type'].should.equal 'application/atom+xml;type=entry'
    end
 
    it 'finds the entry in the given collection' do
      @store.expects(:find_entry).with('articles', 'my_entry')
      do_get
    end
 
    it 'returns the atom representation of the entry' do
      do_get
      body.should.equal @entry.to_s
    end
 
    describe 'When the entry is not found' do
      setup do
        @store.stubs(:find_entry).raises(EntryNotFound)
      end
 
      it 'is not found' do
        get_it '/articles/my_entry'
        should.be.not_found
      end
    end
  end
 
  describe 'PUT /:collection/:entry' do
    def do_put
      put_it '/articles/my_entry', @entry.to_s, :content_type => 'application/atom+xml;type=entry'
    end
 
    setup do
      @entry = stub('an Atom::Entry', :to_s => 'hi i am an atom entry',
        :edit_url => 'http://example.org/my_collection/my_entry')
      @store.stubs(:update_entry).returns(@entry)
    end
 
    it 'is successful' do
      do_put
      should.be.ok
    end
 
    it 'is application/atom+xml;type=entry' do
      do_put
      headers['Content-Type'].should.equal 'application/atom+xml;type=entry'
    end
 
    it 'updates the entry' do
      @store.expects(:update_entry).with('articles', 'my_entry', @entry.to_s).
        returns(@entry)
      do_put
    end
 
    it "sets the Location header to the entry's edit url" do
      do_put
      headers['Location'].should.equal 'http://example.org/my_collection/my_entry'
    end
 
    it 'returns the atom entry' do
      do_put
      body.should.equal 'hi i am an atom entry'
    end
 
    it 'is not found if the collection do not exists' do
      @store.stubs(:update_entry).raises(CollectionNotFound)
      do_put
      should.be.not_found
    end
 
    it 'is not found if the entry do not exists' do
      @store.stubs(:update_entry).raises(EntryNotFound)
      do_put
      should.be.not_found
    end
  end
 
  describe 'DELETE /:collection/:entry' do
    def do_delete
      delete_it '/articles/my_entry'
    end
 
    it 'is success' do
      @store.stubs(:delete_entry)
      do_delete
      should.be.ok
    end
 
    it 'deletes the entry' do
      @store.expects(:delete_entry).with('articles', 'my_entry')
      do_delete
    end
 
    it 'is not found if the entry do not exists' do
      @store.stubs(:delete_entry).raises(EntryNotFound)
      do_delete
      should.be.not_found
    end
 
  end
end