Guest User

Untitled

a guest
Jan 26th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. require File.expand_path('../../../load_paths', __FILE__)
  2.  
  3. lib = File.expand_path("#{File.dirname(__FILE__)}/../lib")
  4. $:.unshift(lib) unless $:.include?('lib') || $:.include?(lib)
  5.  
  6. require 'rubygems'
  7. require 'test/unit'
  8. require 'active_resource'
  9. require 'active_support'
  10. require 'active_support/test_case'
  11.  
  12. require 'setter_trap'
  13.  
  14. require 'logger'
  15. ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log")
  16.  
  17. begin
  18. require 'ruby-debug'
  19. rescue LoadError
  20. end
  21.  
  22. def setup_response
  23. matz_hash = { 'person' => { :id => 1, :name => 'Matz' } }
  24.  
  25. @default_request_headers = { 'Content-Type' => 'application/json' }
  26. @matz = matz_hash.to_json
  27. @matz_xml = matz_hash.to_xml
  28. @david = { :person => { :id => 2, :name => 'David' } }.to_json
  29. @greg = { :person => { :id => 3, :name => 'Greg' } }.to_json
  30. @addy = { :address => { :id => 1, :street => '12345 Street', :country => 'Australia' } }.to_json
  31. @rick = { :person => { :name => "Rick", :age => 25 } }.to_json
  32. @joe = { :person => { :id => 6, :name => 'Joe', :likes_hats => true }}.to_json
  33. @people = { :people => [ { :person => { :id => 1, :name => 'Matz' } },
  34. { :person => { :id => 2, :name => 'David', :hair_color => "black", :age => 32 } }
  35. ]
  36. }.to_json
  37. @people_david = { :people => [ { :person => { :id => 2, :name => 'David' } }] }.to_json
  38. @addresses = { :addresses => [{ :address => { :id => 1, :street => '12345 Street', :country => 'Australia' } }] }.to_json
  39.  
  40. # - deep nested resource -
  41. # - Luis (Customer)
  42. # - JK (Customer::Friend)
  43. # - Mateo (Customer::Friend::Brother)
  44. # - Edith (Customer::Friend::Brother::Child)
  45. # - Martha (Customer::Friend::Brother::Child)
  46. # - Felipe (Customer::Friend::Brother)
  47. # - Bryan (Customer::Friend::Brother::Child)
  48. # - Luke (Customer::Friend::Brother::Child)
  49. # - Eduardo (Customer::Friend)
  50. # - Sebas (Customer::Friend::Brother)
  51. # - Andres (Customer::Friend::Brother::Child)
  52. # - Jorge (Customer::Friend::Brother::Child)
  53. # - Elsa (Customer::Friend::Brother)
  54. # - Natacha (Customer::Friend::Brother::Child)
  55. # - Milena (Customer::Friend::Brother)
  56. #
  57. @luis = {
  58. :customer => {
  59. :id => 1,
  60. :name => 'Luis',
  61. :friends => [{
  62. :name => 'JK',
  63. :brothers => [
  64. {
  65. :name => 'Mateo',
  66. :children => [{ :name => 'Edith' },{ :name => 'Martha' }]
  67. }, {
  68. :name => 'Felipe',
  69. :children => [{ :name => 'Bryan' },{ :name => 'Luke' }]
  70. }
  71. ]
  72. }, {
  73. :name => 'Eduardo',
  74. :brothers => [
  75. {
  76. :name => 'Sebas',
  77. :children => [{ :name => 'Andres' },{ :name => 'Jorge' }]
  78. }, {
  79. :name => 'Elsa',
  80. :children => [{ :name => 'Natacha' }]
  81. }, {
  82. :name => 'Milena',
  83. :children => []
  84. }
  85. ]
  86. }]
  87. }
  88. }.to_json
  89. # - resource with yaml array of strings; for ARs using serialize :bar, Array
  90. @marty = <<-eof.strip
  91. <?xml version=\"1.0\" encoding=\"UTF-8\"?>
  92. <person>
  93. <id type=\"integer\">5</id>
  94. <name>Marty</name>
  95. <colors type=\"yaml\">---
  96. - \"red\"
  97. - \"green\"
  98. - \"blue\"
  99. </colors>
  100. </person>
  101. eof
  102.  
  103. @startup_sound = {
  104. :sound => {
  105. :name => "Mac Startup Sound", :author => { :name => "Jim Reekes" }
  106. }
  107. }.to_json
  108.  
  109. ActiveResource::HttpMock.respond_to do |mock|
  110. mock.get "/people/1.json", {}, @matz
  111. mock.get "/people/1.xml", {}, @matz_xml
  112. mock.get "/people/2.xml", {}, @david
  113. mock.get "/people/5.xml", {}, @marty
  114. mock.get "/people/Greg.json", {}, @greg
  115. mock.get "/people/6.json", {}, @joe
  116. mock.get "/people/4.json", { 'key' => 'value' }, nil, 404
  117. mock.put "/people/1.json", {}, nil, 204
  118. mock.delete "/people/1.json", {}, nil, 200
  119. mock.delete "/people/2.xml", {}, nil, 400
  120. mock.get "/people/99.json", {}, nil, 404
  121. mock.post "/people.json", {}, @rick, 201, 'Location' => '/people/5.xml'
  122. mock.get "/people.json", {}, @people
  123. mock.get "/people/1/addresses.json", {}, @addresses
  124. mock.get "/people/1/addresses/1.json", {}, @addy
  125. mock.get "/people/1/addresses/2.xml", {}, nil, 404
  126. mock.get "/people/2/addresses.json", {}, nil, 404
  127. mock.get "/people/2/addresses/1.xml", {}, nil, 404
  128. mock.get "/people/Greg/addresses/1.json", {}, @addy
  129. mock.put "/people/1/addresses/1.json", {}, nil, 204
  130. mock.delete "/people/1/addresses/1.json", {}, nil, 200
  131. mock.post "/people/1/addresses.json", {}, nil, 201, 'Location' => '/people/1/addresses/5'
  132. mock.get "/people/1/addresses/99.json", {}, nil, 404
  133. mock.get "/people//addresses.xml", {}, nil, 404
  134. mock.get "/people//addresses/1.xml", {}, nil, 404
  135. mock.put "/people//addresses/1.xml", {}, nil, 404
  136. mock.delete "/people//addresses/1.xml", {}, nil, 404
  137. mock.post "/people//addresses.xml", {}, nil, 404
  138. mock.head "/people/1.json", {}, nil, 200
  139. mock.head "/people/Greg.json", {}, nil, 200
  140. mock.head "/people/99.json", {}, nil, 404
  141. mock.head "/people/1/addresses/1.json", {}, nil, 200
  142. mock.head "/people/1/addresses/2.json", {}, nil, 404
  143. mock.head "/people/2/addresses/1.json", {}, nil, 404
  144. mock.head "/people/Greg/addresses/1.json", {}, nil, 200
  145. # customer
  146. mock.get "/customers/1.json", {}, @luis
  147. # sound
  148. mock.get "/sounds/1.json", {}, @startup_sound
  149. end
  150.  
  151. Person.user = nil
  152. Person.password = nil
  153. end
Add Comment
Please, Sign In to add comment