#!/usr/bin/env ruby $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) require 'soaspec' require 'sinatra' require 'sinatra/basic_auth' require 'nokogiri' require 'erb' require 'json' require 'faker' class SoaspecVirtualServer < Sinatra::Application # Used to run virtual web service on localhost. This makes tests more reliable and faster set :port, 4999 # Used to test attributes get '/test_attribute' do Soaspec::TestServer::TestAttribute.note end # Used to test attributes get '/namespace' do # Soaspec::TestServer::TestAttribute.note Soaspec::TestServer::TestNamespace.food end # This is the one being hit by SOAP actions post '/BLZService' do Soaspec::TestServer::GetBank.response_for request end # This is returned when a query for the WSDL is made get '/BLZService' do [200, { 'Content-Type' => 'text/xml' }, Soaspec::TestServer::GetBank.test_wsdl] end # Simulate retrieving an ouath token. Passed to '/invoices' post '/as/token.oauth2' do Soaspec::TestServer::Invoices.user_used = request.env['rack.request.form_hash']['username'] [200, Soaspec::TestServer::Invoices.oauth_headers, JSON.generate(Soaspec::TestServer::Invoices.oauth_body)] end get '/invoice/:id' do |id| JSON.generate(customer_id: id, oauth: request.env['HTTP_AUTHORIZATION'], user: Soaspec::TestServer::Invoices.user_used) end authorize do |username, password| username == 'admin' && password == 'secret' end protect do get '/basic_secrets' do 'Secret data' end end # Used for testing storage of data post '/test/puppy' do request_hash = JSON.parse(request.body.string) id = Soaspec::TestServer::PuppyService.new_id Soaspec::TestServer::PuppyService.data[id][:Name] = request_hash['Name'] Soaspec::TestServer::PuppyService.data[id][:Failure_Type__c] = request_hash['Failure_Type__c'] if request_hash['Failure_Type__c'] response_hash = { result: { Status: 'success', Data: Soaspec::TestServer::PuppyService.data[id] } } JSON.generate response_hash end # Used for testing retrieving storage of data get '/test/puppy/:id' do |id| result = Soaspec::TestServer::PuppyService.data[id.to_i] JSON.generate result end get '/test/multiple_json' do <<-BOOKS {"store": {"bicycle": {"price":19.95, "color":"red"}, "book":[ {"price":8.95, "category":"reference", "title":"Sayings of the Century", "author":"Nigel Rees"}, {"price":12.99, "category":"fiction", "title":"Sword of Honour", "author":"Evelyn Waugh"}, {"price":8.99, "category":"fiction", "isbn":"0-553-21311-3", "title":"Moby Dick", "author":"Herman Melville","color":"blue"}, {"price":22.99, "category":"fiction", "isbn":"0-395-19395-8", "title":"The Lord of the Rings", "author":"Tolkien"} ] } } BOOKS end patch '/test/puppy/:id' do |id| request_hash = JSON.parse(request.body.string) Soaspec::TestServer::PuppyService.data[id.to_i][:Name] = request_hash['Name'] response_hash = { result: { Status: 'updated', With: request_hash['Name'] } } JSON.generate response_hash end end SoaspecVirtualServer.run!