[Groonga-commit] ranguba/chupa-text-http-server at 8677fb1 [master] test: add API error case

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Jul 11 11:06:40 JST 2017


Kouhei Sutou	2017-07-11 11:06:40 +0900 (Tue, 11 Jul 2017)

  New Revision: 8677fb18db31aaacfdc91e98cb01312616f11694
  https://github.com/ranguba/chupa-text-http-server/commit/8677fb18db31aaacfdc91e98cb01312616f11694

  Message:
    test: add API error case

  Modified files:
    app/controllers/extractions_controller.rb
    app/models/extraction.rb
    test/controllers/extractions_controller_test.rb

  Modified: app/controllers/extractions_controller.rb (+5 -3)
===================================================================
--- app/controllers/extractions_controller.rb    2017-07-11 10:25:33 +0900 (7ee15ed)
+++ app/controllers/extractions_controller.rb    2017-07-11 11:06:40 +0900 (0f80f04)
@@ -10,13 +10,15 @@ class ExtractionsController < ApplicationController
     @extraction = Extraction.new(extraction_params)
 
     respond_to do |format|
-      if****@extra*****?
-        @extracted =****@extra*****
+      @extracted =****@extra*****
+      if @extracted
         format.html { render :create }
         format.json { render json: @extracted }
       else
         format.html { render :show }
-        format.json { render json: @extraction.errors, status: :unprocessable_entity }
+        format.json do
+          render json: @extraction.errors, status: :unprocessable_entity
+        end
       end
     end
   end

  Modified: app/models/extraction.rb (+18 -3)
===================================================================
--- app/models/extraction.rb    2017-07-11 10:25:33 +0900 (503fdce)
+++ app/models/extraction.rb    2017-07-11 11:06:40 +0900 (e3c3ab9)
@@ -15,6 +15,8 @@ class Extraction
   end
 
   def extract
+    return nil unless valid?
+
     extractor = ChupaText::Extractor.new
     configuration = ChupaText::Configuration.new
     configuration_loader = ChupaText::ConfigurationLoader.new(configuration)
@@ -29,12 +31,25 @@ class Extraction
       end
       data = ChupaText::VirtualFileData.new(data_uri, @data.to_io)
     else
-      data = ChupaText::InputData.new(@uri)
+      begin
+        data = ChupaText::InputData.new(@uri)
+      rescue ChupaText::DownloadError => error
+        errors.add(:uri, :invalid, message: error.message)
+        return nil
+      rescue => error
+        errors.add(:uri, :invalid, message: "#{error.class}: #{error.message}")
+        return nil
+      end
     end
     formatter = ChupaText::Formatters::Hash.new
     formatter.format_start(data)
-    extractor.extract(data) do |extracted|
-      formatter.format_extracted(extracted)
+    begin
+      extractor.extract(data) do |extracted|
+        formatter.format_extracted(extracted)
+      end
+    rescue ChupaText::Error => error
+      errors.add(:data, :invalid, message: error.message)
+      return nil
     end
     formatter.format_finish(data)
   end

  Modified: test/controllers/extractions_controller_test.rb (+45 -3)
===================================================================
--- test/controllers/extractions_controller_test.rb    2017-07-11 10:25:33 +0900 (064d3c8)
+++ test/controllers/extractions_controller_test.rb    2017-07-11 11:06:40 +0900 (51432c5)
@@ -4,7 +4,7 @@ class ExtractionsControllerTest < ActionDispatch::IntegrationTest
   setup do
   end
 
-  def run_http_server(content_type: nil, body: nil)
+  def run_http_server(content_type: nil, body: nil, status: nil)
     config = {
       :Port => 0,
       :Logger => Rails.logger,
@@ -13,16 +13,18 @@ class ExtractionsControllerTest < ActionDispatch::IntegrationTest
       ],
     }
     server = WEBrick::HTTPServer.new(config)
-    server.mount_proc("/") do |request, response|
+    path = "/data"
+    server.mount_proc(path) do |request, response|
       response.content_type = content_type
       response.body = body
+      response.status = status if status
     end
     server_thread = Thread.new do
       server.start
     end
     begin
       port = server[:Port]
-      yield("http://127.0.0.1:#{port}/")
+      yield("http://127.0.0.1:#{port}#{path}")
     ensure
       server.shutdown
       server_thread.join
@@ -175,6 +177,46 @@ class ExtractionsControllerTest < ActionDispatch::IntegrationTest
           assert_equal(["Hello"], extracted)
         end
       end
+
+      test "not found" do
+        run_http_server(content_type: "text/plain", body: "Hello") do |uri|
+          nonexistent_uri = URI.parse(uri)
+          nonexistent_uri.path = "/nonexistent"
+          post(extraction_url(format: "json"),
+               params: {
+                 uri: nonexistent_uri.to_s,
+               })
+          assert_response(:unprocessable_entity)
+          assert_equal("application/json", response.content_type,
+                       response.body)
+          assert_equal({
+                         "uri" => [
+                           "Download error: <#{nonexistent_uri}>: 404 Not Found",
+                         ],
+                       },
+                       JSON.parse(response.body))
+        end
+      end
+
+      test "internal server error" do
+        run_http_server(content_type: "text/plain",
+                        body: "Error",
+                        status: 500) do |uri|
+          post(extraction_url(format: "json"),
+               params: {
+                 uri: uri,
+               })
+          assert_response(:unprocessable_entity)
+          assert_equal("application/json", response.content_type,
+                       response.body)
+          assert_equal({
+                         "uri" => [
+                           "Download error: <#{uri}>: 500 Internal Server Error",
+                         ],
+                       },
+                       JSON.parse(response.body))
+        end
+      end
     end
 
     sub_test_case "data" do
-------------- next part --------------
HTML����������������������������...
Télécharger 



More information about the Groonga-commit mailing list
Back to archive index