Create tests for Rest objects

Unofficial Content

This feature allows testing procedures exposed as REST services by calling them via HTTP.

Test Creation

There are two ways of creating REST Test objects.

The first and recommended option is to use the procedure context menu's Create REST Test option :
createRESTcontextMenu_png

If at the time of creation, you have not set up the current environment's REST Test Base URL property, it will be automatically generated and set for you using the environment back end's Web Root.

This will automatically generate a REST Test object, similar to how Unit Test objects are created from within this context menu, meaning that alongside your REST Test, you will find an associated DataProvider object to load all of the data the Test will depend upon, as well as an SDT, describing that structure's data.

RESTvsUnitSDTdifference_2022715111820_1_png

The main differences between this generated REST Test and a generated Unit Test will be that the SDT will contain an extra field for the expected response's HTTP status code, and in the test object source code and variables.

The test will be generated with at least three extra variables:

  • &HttpClient: the variable to load the information and execute the remote call to the service under test
  • &RequestBody: a variable to load the body of the HTTP request
  • &RestSettings: a variable to load the environment's base URL

However, if the application has enabled integrated GAM security, even more variables will be generated to accomplish authentication within the test:

  • &GAMApplication: used for obtaining ClientId and ClientSecret
  • &LoginAdditionalParameters: object that holds additional login params such as authentication type
  • &OAuthAdditionalParameters: object that holds additional login params such as credentials' scope
  • &AccessToken: a variable to store the OAuth access token after logging in

As before, if the application has enabled integrated GAM security, some extra fields will be present in the generated SDT, namely, User and Password.

We recommend that you take a second to edit the generated test data, since you HAVE to provide the credentials for user and password fields. While doing that, we strongly suggest that you at least test another status code, so for example, set ExpectedStatusCode to 200 when providing correct credentials, and set it to 401 when leaving them empty.

RESTdataSample_png

Example REST Test without GAM Authentication enabled

/* Autogenerated REST test code for Procedure 'Procedure1' */

Do 'Prepare Client'

For &TestCaseData in Procedure1RESTTestData()
    &RequestBody = '{ "inParam": ' + &TestCaseData.inParam.ToString() + '}'
    &RequestBody += '{ "outParam": ' + &TestCaseData.outParam.ToString() + '}'
    
    &HttpClient.AddString(&RequestBody)
    
    /* Act...                       */
    &HttpClient.Execute("POST", ExampleProcedure.Type)
    
    // Load &HttpClient response on TestCaseData
    &TestCaseData.FromJson(&HttpClient.ToString())
    
    /* Assert...                    */
    AssertNumericEquals(&TestCaseData.ExpectedStatusCode, &HttpClient.StatusCode, format(!'%1.ExpectedStatusCode:', &TestCaseData.TestCaseId))
    AssertNumericEquals(&TestCaseData.ExpectedoutParam, &TestCaseData.outParam, format(!'%1.ExpectedoutParam: %2', &TestCaseData.TestCaseId, &TestCaseData.MsgoutParam))
endfor

Sub 'Prepare Client'
    &HttpClient.Host = &RestSettings.Host
    &HttpClient.Port = &RestSettings.Port
    &HttpClient.Secure = &RestSettings.Secure
    &HttpClient.BaseUrl = &RestSettings.BaseURL
    &HttpClient.AddHeader('Content-type', 'application/json')
EndSub

Example REST Test with GAM Authentication enabled

/* Autogenerated REST test code for Procedure 'Procedure1' */

Do 'Prepare Client'

For &TestCaseData in Procedure1RESTTest2Data()
    &AccessToken = GAMRepository.GetOauthAccessToken(&TestCaseData.User, &TestCaseData.Password, &LoginAdditionalParameters, &OAuthAdditionalParameters, &GAMSession, &GAMErrors)
    &HttpClient.AddHeader('Authorization', 'OAuth ' + &AccessToken.access_token)
    
    &RequestBody = '{ "inParam": ' + &TestCaseData.inParam.ToString() + '}'
    &RequestBody += '{ "outParam": ' + &TestCaseData.outParam.ToString() + '}'
    
    &HttpClient.AddString(&RequestBody)
    
    /* Act...                       */
    &HttpClient.Execute("POST", ExampleProcedure.Type)
    
    // Load &HttpClient response on TestCaseData
    &TestCaseData.FromJson(&HttpClient.ToString())
    
    /* Assert...                    */
    AssertNumericEquals(&TestCaseData.ExpectedStatusCode, &HttpClient.StatusCode, format(!'%1.ExpectedStatusCode:', &TestCaseData.TestCaseId))
    AssertNumericEquals(&TestCaseData.ExpectedoutParam, &TestCaseData.outParam, format(!'%1.ExpectedoutParam: %2', &TestCaseData.TestCaseId, &TestCaseData.MsgoutParam))
endfor

Sub 'Prepare Client'
    &HttpClient.Host = &RestSettings.Host
    &HttpClient.Port = &RestSettings.Port
    &HttpClient.Secure = &RestSettings.Secure
    &HttpClient.BaseUrl = &RestSettings.BaseURL
    &HttpClient.AddHeader('Content-type', 'application/json')
    &GAMApplication = GAMApplication.Get()
    &LoginAdditionalParameters.AuthenticationTypeName = "local"
    &OAuthAdditionalParameters.ClientId = &GAMApplication.ClientId
    &OAuthAdditionalParameters.ClientSecret = &GAMApplication.ClientSecret
    &OAuthAdditionalParameters.Scope = "FullControl"
EndSub