diff --git a/spec_integration/.bundle/config b/spec_integration/.bundle/config new file mode 100644 index 0000000..57d4641 --- /dev/null +++ b/spec_integration/.bundle/config @@ -0,0 +1,3 @@ +--- +BUNDLE_PATH: vendor/bundle +BUNDLE_DISABLE_SHARED_GEMS: '1' diff --git a/spec_integration/Gemfile b/spec_integration/Gemfile new file mode 100644 index 0000000..62bb1c8 --- /dev/null +++ b/spec_integration/Gemfile @@ -0,0 +1,8 @@ +# A sample Gemfile +source "https://rubygems.org" + +gem "bundler" + +group :test do + gem 'rspec' +end diff --git a/spec_integration/config/webapi.conf.example b/spec_integration/config/webapi.conf.example new file mode 100644 index 0000000..b8e90b9 --- /dev/null +++ b/spec_integration/config/webapi.conf.example @@ -0,0 +1 @@ +uri: 'http://localhost:8080' diff --git a/spec_integration/spec/helpers/logger.rb b/spec_integration/spec/helpers/logger.rb new file mode 100644 index 0000000..9c5e131 --- /dev/null +++ b/spec_integration/spec/helpers/logger.rb @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- + +require 'logger' + +module Helpers + module Logger + class CustomLogger + attr_reader :logger + + def initialize(progname) + @progname = progname + end + + ["fatal", "error", "warn", "info", "debug"].each do |level| + define_method(level) { |msg| + logger.__send__(level, "#{msg}") + } + end + alias :warning :warn + + def logger + l = ::Logger.new(::Logger::LogDevice.new($>)) + l.progname = @progname + l + end + + end + + def self.included(klass) + klass.class_eval { + + @class_logger = CustomLogger.new(self.to_s.split('::').last) + + def self.logger + @class_logger + end + + def logger + self.class.logger + end + + def self.logger_name + @class_logger.progname + end + + def self.logger_name=(name) + @class_logger.progname = name + end + } + end + + end +end diff --git a/spec_integration/spec/helpers/web_api_client.rb b/spec_integration/spec/helpers/web_api_client.rb new file mode 100644 index 0000000..5d9b60e --- /dev/null +++ b/spec_integration/spec/helpers/web_api_client.rb @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- + +require 'json' +require 'net/http' + +require_relative 'web_api_client/api_resource' +require_relative 'web_api_client/response_format' + +require_relative 'web_api_client/api_resources/0.0.1/comments' + +module WebAPIClient + def self.uri=(u) + ApiResource.api_uri = u + end + + def self.version=(v) + ApiResource.api_version = v + end + + def self.format=(f) + ApiResource.api_format = f + end +end + +# Set default values +WebAPIClient.uri = 'http://localhost:8080' +WebAPIClient.version = '0.0.1' +WebAPIClient.format = :json + diff --git a/spec_integration/spec/helpers/web_api_client/api_resource.rb b/spec_integration/spec/helpers/web_api_client/api_resource.rb new file mode 100644 index 0000000..e3e3f1e --- /dev/null +++ b/spec_integration/spec/helpers/web_api_client/api_resource.rb @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- + +module WebAPIClient + class ApiResource + class << self + attr_accessor :api_uri + attr_accessor :api_version + attr_accessor :api_format + + def api_full_uri(suffix) + u = ApiResource.api_uri + v = ApiResource.api_version + f = ApiResource.api_format + + uri = "#{u}/api/#{v}" + uri += "/#{suffix}" if suffix + uri += ".#{f}" + + URI(uri) + end + + def send_request(verb, suffix, params = nil) + uri = api_full_uri(suffix) + uri.query = URI.encode_www_form(params) if params + + response = Net::HTTP.start(uri.host, uri.port) do |http| + request = verb.new(uri.request_uri) + http.request(request) + end + + response_format = ApiResource.api_format.to_sym + + ResponseFormats[response_format].parse(response) + end + end + end +end diff --git a/spec_integration/spec/helpers/web_api_client/api_resources/0.0.1/comments.rb b/spec_integration/spec/helpers/web_api_client/api_resources/0.0.1/comments.rb new file mode 100644 index 0000000..6dab7a9 --- /dev/null +++ b/spec_integration/spec/helpers/web_api_client/api_resources/0.0.1/comments.rb @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +module WebAPIClient + class Comment < ApiResource + class << self + def index + send_request(Net::HTTP::Get, "comments") + end + + def show(id) + send_request(Net::HTTP::Get, "comments/#{id}") + end + + def create(params = nil) + send_request(Net::HTTP::Post, "comments", params) + end + + def update(id, params = nil) + send_request(Net::HTTP::Put, "comments/#{id}", params) + end + + def delete(id) + send_request(Net::HTTP::Delete, "comments/#{id}") + end + end + end +end diff --git a/spec_integration/spec/helpers/web_api_client/response_format.rb b/spec_integration/spec/helpers/web_api_client/response_format.rb new file mode 100644 index 0000000..cbd1a78 --- /dev/null +++ b/spec_integration/spec/helpers/web_api_client/response_format.rb @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +module WebAPIClient + module ResponseFormats + def self.[](format) + case format + when :json + Json.new + else + rase "Unknown response format: #{format}" + end + end + + class Format + def parse(response) + raise NotImplementedError + end + end + + class Json < Format + def parse(response) + JSON.parse(response.body) unless response.body.empty? + end + end + end +end diff --git a/spec_integration/spec/spec_helper.rb b/spec_integration/spec/spec_helper.rb new file mode 100644 index 0000000..0821bd7 --- /dev/null +++ b/spec_integration/spec/spec_helper.rb @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- + +require 'rubygems' +require 'bundler' +Bundler.setup(:default) +Bundler.require(:test) + +$LOAD_PATH << File.expand_path("./helpers", File.dirname(__FILE__)) + +require 'web_api_client' + +require_relative 'helpers/logger' + +RSpec.configure do |c| + c.formatter = :documentation + c.color = true +end + + diff --git a/spec_integration/spec/webapi_integration_spec.rb b/spec_integration/spec/webapi_integration_spec.rb new file mode 100644 index 0000000..a100854 --- /dev/null +++ b/spec_integration/spec/webapi_integration_spec.rb @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- + +require 'yaml' +require 'spec_helper' + +describe 'Webapi Integration spec' do + before do + webapi_config = YAML.load_file(File.expand_path('./config/webapi.conf')) + WebAPIClient.uri = webapi_config["uri"] + end + + let(:comment_params) do + { + display_name: "webapitest", + comment: "webapi sample message", + } + end + + let(:comment) { WebAPIClient::Comment.create(comment_params) } + + describe 'post' do + it 'create a new comment' do + res = WebAPIClient::Comment.create(comment_params) + expect(res["display_name"]).to eq comment_params[:display_name] + end + end + + describe 'get' do + it 'show list for the comments' do + res = WebAPIClient::Comment.index + expect(res).to be_a(Array) + end + + it 'show detail the comment' do + res = WebAPIClient::Comment.show(comment["id"]) + expect(res["id"]).to eq comment["id"] + end + end + + # describe 'update' do + # before do + # comment_params[:comment] = "webapi sample message 2" + # end + # it 'update api not allowed' do + # res = WebAPIClient::Comment.update(comment["id"], comment_params) + # expect(res["code"]).to eq 405 + # end + # end + + # describe 'delete' do + # it 'delete api not allowed' do + # res = WebAPIClient::Comment.delete(comment["id"]) + # expect(res["code"]).to eq 405 + # end + # end +end