/*
** Module   : HttpResponseStatus.java
** Abstract : A test for the HTTP response status.
**
** Copyright (c) 2021, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
** 001 CA  20210827 Created initial version.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness.test;

import org.apache.http.*;

import com.goldencode.harness.*;

/**
 * A test applied to the HTTP response status.
 */
public class HttpResponseStatus
extends Report
implements WebResponseTestable
{
   /** The expected status code. */
   private int code;
   
   /** The expected status reason. */
   private String reason;

   /**
    * A test expecting the specified code and reason in the response.
    * 
    * @param    code
    *           The expected status code.
    * @param    reason
    *           The expected status reason.
    */
   public HttpResponseStatus(String code, String reason)
   {
      this.code = Integer.parseInt(code);
      this.reason = reason;
   }

   /**
    * Verify the response status code and reason match.
    * 
    * @param    response
    *           The HTTP response.
    */
   @Override
   public void execute(HttpResponse response)
   {
      StatusLine sl = response.getStatusLine();
      
      setStatus(Status.PASSED, null);
      
      if (code != sl.getStatusCode())
      {
         setStatus(Status.FAILED, 
                   "Status code failed: expected " + code + " and received " + sl.getStatusCode());
      }

      if (reason != null && !reason.equals(sl.getReasonPhrase()))
      {
         setStatus(Status.FAILED, 
                   "Status reason failed: expected " + reason + " and received " + sl.getReasonPhrase());
      }
   }
}
