Forum Discussion

TDYSmartBear's avatar
TDYSmartBear
Occasional Contributor
6 years ago
Solved

Use regular expression on ReadyAPI event target

I'm trying to use different RequestFilter.filterRequest to add headers to different tests. Not sure how the regular expression should be used in the target field.

 

I'd like to use RequestFilter1 for all GET requests and RequestFilter2 for all SAVE/DELETE requests. I tried to set  RequestFilter1 with target"*Service.get*" and RequestFilter2 with target "*Service.save*". But did not work. Did I do anything wrong here?

 

Thank you for your input in advance!

 

TestSuite:

|--AppleService

    |--getApples

    |--getApple

    |--saveApple

    |--deleteApple

|--BService

    |--getBananas

    |--getBanana

    |--saveBanana

    |--deleteBanana

...

 

  • I am using ReadyAPI 2.2.0 and haven't upgraded yet.  I don't see a "event dialogue" so I'm not sure what you mean. But From the Events button at the upper right, next to the bell icon, click the button.

     

    The Events window opens. Use "+" to add RequestFilter.filterRequest - I thought from your original message you already know this stuff. 

     

    In your groovy script - assuming you use the GET processing like the above code I suggested,

     

    def headers = request.requestHeaders
    
    String theMethod = context.getProperty("httpMethod").toString()
    
    String theGet = new String("GET")
    
    def namelist = context.getPropertyNames()
    
    // EXAMPLE: Set header to application/x-www-form-urlencoded in case GET
    
    // clear out request content, set header Content-Type to application/x-www-form/urlencoded
    if (theMethod.substring(0, 3) == theGet) 
    {
    
         // Clear out the request content. Actually this probably fullfills the same
         // as "Remove Empty Content" in the REST Request Properties: tab
         context.setProperty("requestContent", '')
         request.requestContent = ''
    
    	log.info "headers (right before adding  the urlencoded content type): " + headers
    
    	log.info "headers size now: " + (request.requestHeaders.size())
    
    	headers.put( "Content-Type", "application/x-www-form-urlencoded") // Replace values with those you need
    
    	log.info "Added Content-Type: application/x-www-form-urlencoded"
    
            // finish setting the header...
    	request.requestHeaders = headers
    
    	log.info "headers size now: " + (request.requestHeaders.size())
    
    	// display the header
    	log.info "actual header is " + request.requestHeaders
    
    }
    else
    {
    //blah blah blah 2
    }

4 Replies

  • Why not use one RequestFilter.filterRequest event? Then ssearch for the httpMethod "GET"  or "DELETE" and use conditional statements to do the processing according to the method?

     

    log.info "\n\n\n\n"
    log.info "                  This is the RequestFilter.filterRequest Groovy Script running now "
    log.info "\n\n\n\n"
    
    // Figured how to narrow the scope of this event handler to the GET methods
    String theMethod = context.getProperty("httpMethod").toString()
    
    String theGet = new String("GET")
    String theDelete - new String("DELETE")
    
    // Only do this stuff if method is GET
    // clear out request content, set header Content-Type to application/x-www-form/urlencoded
    if (theMethod.substring(0, 3) == theGet) 
    {
       // blah blah blah 1
    }
    else if (theMethod.substring(0,6) == theDelete)
    {
      // blah blah blah 2
    }

     

    • TDYSmartBear's avatar
      TDYSmartBear
      Occasional Contributor

      Thank you for the prompt response. Any way to do it from the ReadyAPI event dialogue?

      • Bill_In_Irvine's avatar
        Bill_In_Irvine
        Contributor

        I am using ReadyAPI 2.2.0 and haven't upgraded yet.  I don't see a "event dialogue" so I'm not sure what you mean. But From the Events button at the upper right, next to the bell icon, click the button.

         

        The Events window opens. Use "+" to add RequestFilter.filterRequest - I thought from your original message you already know this stuff. 

         

        In your groovy script - assuming you use the GET processing like the above code I suggested,

         

        def headers = request.requestHeaders
        
        String theMethod = context.getProperty("httpMethod").toString()
        
        String theGet = new String("GET")
        
        def namelist = context.getPropertyNames()
        
        // EXAMPLE: Set header to application/x-www-form-urlencoded in case GET
        
        // clear out request content, set header Content-Type to application/x-www-form/urlencoded
        if (theMethod.substring(0, 3) == theGet) 
        {
        
             // Clear out the request content. Actually this probably fullfills the same
             // as "Remove Empty Content" in the REST Request Properties: tab
             context.setProperty("requestContent", '')
             request.requestContent = ''
        
        	log.info "headers (right before adding  the urlencoded content type): " + headers
        
        	log.info "headers size now: " + (request.requestHeaders.size())
        
        	headers.put( "Content-Type", "application/x-www-form-urlencoded") // Replace values with those you need
        
        	log.info "Added Content-Type: application/x-www-form-urlencoded"
        
                // finish setting the header...
        	request.requestHeaders = headers
        
        	log.info "headers size now: " + (request.requestHeaders.size())
        
        	// display the header
        	log.info "actual header is " + request.requestHeaders
        
        }
        else
        {
        //blah blah blah 2
        }