Search code examples
pythonjythonparasoft

Return NULL on mismatch


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"


Solution

  • 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