<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,15 +1,25 @@
 class SearchController &lt; ApplicationController
-  
+
   def results
     if params[:q].chomp.blank?
-      redirect_to root_path
+      respond_to do |format|
+        format.html { redirect_to root_path }
+        format.json { render :status =&gt; 404, :json =&gt; { :error =&gt; &quot;Missing 'q' parameter&quot; } }
+      end
     else
       @results = Domain.search(params[:q], params[:page], current_user)
-      
-      if @results.size == 1
-        redirect_to domain_path(@results.pop)
+
+      respond_to do |format|
+        format.html do
+          if @results.size == 1
+            redirect_to domain_path(@results.pop)
+          end
+        end
+        format.json do
+          render :json =&gt; @results.to_json(:only =&gt; [:id, :name])
+        end
       end
     end
   end
-  
+
 end</diff>
      <filename>app/controllers/search_controller.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,9 +1,10 @@
 require File.dirname(__FILE__) + '/../spec_helper'
 
-describe SearchController do
+describe SearchController, &quot;for admins&quot; do
 
   before(:each) do
-    session[:user_id] = Factory(:admin).id
+    #session[:user_id] = Factory(:admin).id
+    login_as Factory(:admin)
 
     Factory(:domain, :name =&gt; 'example.com')
     Factory(:domain, :name =&gt; 'example.net')
@@ -40,3 +41,32 @@ describe SearchController do
   end
 
 end
+
+describe SearchController, &quot;for api clients&quot; do
+  before(:each) do
+    authorize_as(Factory(:api_client))
+
+    Factory(:domain, :name =&gt; 'example.com')
+    Factory(:domain, :name =&gt; 'example.net')
+  end
+
+  it &quot;should return an empty JSON response for no results&quot; do
+    get :results, :q =&gt; 'amazon', :format =&gt; 'json'
+
+    assigns[:results].should be_empty
+
+    response.should have_text(&quot;[]&quot;)
+  end
+
+  it &quot;should return a JSON set of results&quot; do
+    get :results, :q =&gt; 'example', :format =&gt; 'json'
+
+    assigns[:results].should_not be_empty
+
+    json = ActiveSupport::JSON.decode( response.body )
+    json.size.should be(2)
+    json.first[&quot;domain&quot;].keys.should include('id', 'name')
+    json.first[&quot;domain&quot;][&quot;name&quot;].should match(/example/)
+    json.first[&quot;domain&quot;][&quot;id&quot;].to_s.should match(/\d+/)
+  end
+end</diff>
      <filename>spec/controllers/search_controller_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>0b9ef3ef423c5c579c5b1e8bef07b3822ba35428</id>
    </parent>
  </parents>
  <author>
    <name>Kenneth Kalmer</name>
    <email>kenneth.kalmer@gmail.com</email>
  </author>
  <url>http://github.com/kennethkalmer/powerdns-on-rails/commit/aac8aaef18d648796f4fd80283e687b16253eab0</url>
  <id>aac8aaef18d648796f4fd80283e687b16253eab0</id>
  <committed-date>2009-09-26T13:22:23-07:00</committed-date>
  <authored-date>2009-09-26T13:22:23-07:00</authored-date>
  <message>JSON search results</message>
  <tree>691b2a28c2f080a172a412d159c89cae83180cb4</tree>
  <committer>
    <name>Kenneth Kalmer</name>
    <email>kenneth.kalmer@gmail.com</email>
  </committer>
</commit>
