@@ -1,83 +1,94 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require 'blankslate' |
|
19 | 19 | |
|
20 | 20 | module Redmine |
|
21 | 21 | module Views |
|
22 | 22 | module Builders |
|
23 | 23 | class Structure < BlankSlate |
|
24 | 24 | attr_accessor :request, :response |
|
25 | 25 | |
|
26 | 26 | def initialize(request, response) |
|
27 | 27 | @struct = [{}] |
|
28 | 28 | self.request = request |
|
29 | 29 | self.response = response |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | def array(tag, options={}, &block) |
|
33 | 33 | @struct << [] |
|
34 | 34 | block.call(self) |
|
35 | 35 | ret = @struct.pop |
|
36 | 36 | @struct.last[tag] = ret |
|
37 | 37 | @struct.last.merge!(options) if options |
|
38 | 38 | end |
|
39 | 39 | |
|
40 | def encode_value(value) | |
|
41 | if value.is_a?(Time) | |
|
42 | # Rails uses a global setting to format JSON times | |
|
43 | # Don't rely on it for the API as it could have been changed | |
|
44 | value.xmlschema(0) | |
|
45 | else | |
|
46 | value | |
|
47 | end | |
|
48 | end | |
|
49 | ||
|
40 | 50 | def method_missing(sym, *args, &block) |
|
41 | 51 | if args.any? |
|
42 | 52 | if args.first.is_a?(Hash) |
|
43 | 53 | if @struct.last.is_a?(Array) |
|
44 | 54 | @struct.last << args.first unless block |
|
45 | 55 | else |
|
46 | 56 | @struct.last[sym] = args.first |
|
47 | 57 | end |
|
48 | 58 | else |
|
59 | value = encode_value(args.first) | |
|
49 | 60 | if @struct.last.is_a?(Array) |
|
50 | 61 | if args.size == 1 && !block_given? |
|
51 |
@struct.last << |
|
|
62 | @struct.last << value | |
|
52 | 63 | else |
|
53 |
@struct.last << (args.last || {}).merge(:value => |
|
|
64 | @struct.last << (args.last || {}).merge(:value => value) | |
|
54 | 65 | end |
|
55 | 66 | else |
|
56 |
@struct.last[sym] = |
|
|
67 | @struct.last[sym] = value | |
|
57 | 68 | end |
|
58 | 69 | end |
|
59 | 70 | end |
|
60 | 71 | |
|
61 | 72 | if block |
|
62 | 73 | @struct << (args.first.is_a?(Hash) ? args.first : {}) |
|
63 | 74 | block.call(self) |
|
64 | 75 | ret = @struct.pop |
|
65 | 76 | if @struct.last.is_a?(Array) |
|
66 | 77 | @struct.last << ret |
|
67 | 78 | else |
|
68 | 79 | if @struct.last.has_key?(sym) && @struct.last[sym].is_a?(Hash) |
|
69 | 80 | @struct.last[sym].merge! ret |
|
70 | 81 | else |
|
71 | 82 | @struct.last[sym] = ret |
|
72 | 83 | end |
|
73 | 84 | end |
|
74 | 85 | end |
|
75 | 86 | end |
|
76 | 87 | |
|
77 | 88 | def output |
|
78 | 89 | raise "Need to implement #{self.class.name}#output" |
|
79 | 90 | end |
|
80 | 91 | end |
|
81 | 92 | end |
|
82 | 93 | end |
|
83 | 94 | end |
@@ -1,37 +1,47 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class Redmine::ApiTest::ApiTest < Redmine::ApiTest::Base |
|
21 | 21 | fixtures :users |
|
22 | 22 | |
|
23 | 23 | def test_api_should_work_with_protect_from_forgery |
|
24 | 24 | ActionController::Base.allow_forgery_protection = true |
|
25 | 25 | assert_difference('User.count') do |
|
26 | 26 | post '/users.xml', { |
|
27 | 27 | :user => { |
|
28 | 28 | :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', |
|
29 | 29 | :mail => 'foo@example.net', :password => 'secret123'} |
|
30 | 30 | }, |
|
31 | 31 | credentials('admin') |
|
32 | 32 | assert_response 201 |
|
33 | 33 | end |
|
34 | 34 | ensure |
|
35 | 35 | ActionController::Base.allow_forgery_protection = false |
|
36 | 36 | end |
|
37 | end No newline at end of file | |
|
37 | ||
|
38 | def test_json_datetime_format | |
|
39 | get '/users/1.json', {}, credentials('admin') | |
|
40 | assert_include '"created_on":"2006-07-19T17:12:21Z"', response.body | |
|
41 | end | |
|
42 | ||
|
43 | def test_xml_datetime_format | |
|
44 | get '/users/1.xml', {}, credentials('admin') | |
|
45 | assert_include '<created_on>2006-07-19T17:12:21Z</created_on>', response.body | |
|
46 | end | |
|
47 | end |
General Comments 0
You need to be logged in to leave comments.
Login now