File: provides.rb

package info (click to toggle)
libinnate-ruby 2010.07-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 812 kB
  • ctags: 621
  • sloc: ruby: 4,242; makefile: 2
file content (105 lines) | stat: -rw-r--r-- 2,810 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
require File.expand_path('../../helper', __FILE__)

require 'json'
require 'yaml'

Innate.options.merge!(:views => 'provides', :layouts => 'provides')

class SpecNodeProvides
  Innate.node '/'

  provide(:html, :engine => :None)
  provide(:yaml, :type => 'text/yaml'){|a,s| s.to_yaml }
  provide(:json, :type => 'application/json'){|a,s| s.to_json }

  def object
    {'intro' => 'Hello, World!'}
  end

  def string
    'Just 42'
  end

  def args(*args)
    args
  end
end

class SpecNodeProvidesTemplates
  Innate.node '/template'
  map_views '/'

  provide(:yaml, :type => 'text/yaml'){|a,s| s.to_yaml }
  provide(:json, :type => 'application/json'){|a,s| s.to_json }
  provide(:txt, :engine => :Etanni, :type => 'text/plain')

  def list
    @users = %w[starbucks apollo athena]
  end
end

shared :assert_wish do
  def assert_wish(uri, body, content_type)
    got = get(uri)
    got.status.should == 200
    got.body.strip.should == body.strip
    got['Content-Type'].should == content_type
  end
end

describe 'Content representation' do
  describe 'without template' do
    behaves_like :rack_test, :assert_wish

    it 'provides yaml for an object' do
      assert_wish('/object.yaml', {'intro' => 'Hello, World!'}.to_yaml, 'text/yaml')
    end

    it 'provides json for an object' do
      assert_wish('/object.json', {'intro' => 'Hello, World!'}.to_json, 'application/json')
    end

    it 'provides html for an object' do
      assert_wish('/string.html', 'Just 42', 'text/html')
    end

    it 'defaults to html presentation' do
      assert_wish('/string', 'Just 42', 'text/html')
    end

    it 'takes arguments with <name>/arg1/arg2.json' do
      assert_wish('/args', '[]', 'text/html')
      assert_wish('/args.json', '[]', 'application/json')
      assert_wish('/args/a/b/c.json', '["a","b","c"]', 'application/json')
    end
  end

  describe 'with templates' do
    behaves_like :rack_test, :assert_wish

    it 'defaults to <name>.html.<engine>' do
      body = '<ul><li>starbucks</li><li>apollo</li><li>athena</li></ul>'
      assert_wish('/template/list', body, 'text/html')
    end

    it 'uses explicit wish for <name>.html.<engine>' do
      body = '<ul><li>starbucks</li><li>apollo</li><li>athena</li></ul>'
      assert_wish('/template/list.html', body, 'text/html')
    end

    it 'fails when the wish cannot be satisfied' do
      got = get('/template/list.svg')
      got.status.should == 404
    end

    it 'uses the object returned from the action method for block provides' do
      body = %w[starbucks apollo athena].to_yaml
      assert_wish('/template/list.yaml', body, 'text/yaml')
    end

    it 'uses explicit wish for <name>.txt.<engine>' do
      body = "starbucks\napollo\nathena"
      assert_wish('/template/list.txt', body, 'text/plain')
    end
  end
end