1

The below code fetches an input and splits the input at value "HttpOnly" and there after if an "if" condition is satisfied then it returns the value as such.

How can I make the value to return as NULL or "123" if the condition fails at split() itself ?

from soaptest.api import *
from com.parasoft.api import *

def getHeader(input, context):

    headerNew = ""
    strHeader = str(input).split("HttpOnly")
    for i in strHeader:
        if "com.abc.mb.SSO_GUID" in i:
            Application.showMessage(i)
            headerNew = i

    return headerNew

EDIT

Input - "abcdefgHttpOnly"

Output - "abcdefg"

Input - "abcdefg"

Output - "123"

  • 1
    You mean if 'HttpOnly' does not exist in `input`? `split()` can't "fail" if called on a string, it will return a list with one item if the argument passed does not exist in the string. – Endyd Mar 01 '19 at 13:57
  • yes, I have a response that returns a value cookie "abc.defHttpOnly" when the request is a success, but when the request fails I do not get anything so I need "123" value to be generated rather – Santhosh Test Mar 01 '19 at 14:00

1 Answers1

0

You can just test if 'HttpOnly' is in input first and return '123'.

def getHeader(input):
    if 'HttpOnly' not in str(input):
        return '123'

    headerNew = ""
    strHeader = str(input).split("HttpOnly")

    # Not using i as variable since it is usually used as an index
    for header in strHeader:
        if "com.abc.mb.SSO_GUID" in header:
            # Application.showMessage(header)
            headerNew = header

    return headerNew
print(getHeader('com.abc.mb.SSO_GUIDabcdefgHttpOnly')) # com.abc.mb.SSO_GUIDabcdefg
print(getHeader('com.abc.mb.SSO_GUIDabcdefg')) # 123
Endyd
  • 1,249
  • 6
  • 12
  • Pardon my ignorance but this does not seem to return the expected http://tpcg.io/cDFaUj – Santhosh Test Mar 01 '19 at 14:17
  • Can you provide some sample `input`s and expected output? – Endyd Mar 01 '19 at 14:21
  • I'm not sure what you mean when you say it does not seem to return the expected. It's passing the two test cases you gave (I added the "com.abc.mb.SSO_GUID" part since that was in your original code). – Endyd Mar 01 '19 at 14:52