Table of contents

Contents



Official 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 option is to create them as any other object from the New Object window.

The second 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.

RestvsUnitUpdated_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.

RESTdataSampleUpdated_png

Example Rest Test without GAM Authentication enabled

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

Do 'Prepare Client'

For &TestCaseData in RestDemoProcedureRestTestData2()
    
    &HttpClient.AddHeader(!'Content-type', !'application/json')
    &RequestBody = '{  "inParam": ' + &TestCaseData.inParam.ToString() + '}'
    
    &HttpClient.AddString(&RequestBody)
    
    /* Act...                       */
    &HttpClient.Execute(!"POST", RestDemoProcedure.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
EndSub

Example Rest Test with GAM Authentication enabled

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

Do 'Prepare Client'

For &TestCaseData in RestDemoProcedureRestTestData()
    
    &HttpClient.AddHeader(!'Content-type', !'application/json')
    &AccessToken = GAMRepository.GetOauthAccessToken(&TestCaseData.User, &TestCaseData.Password, &LoginAdditionalParameters, &OAuthAdditionalParameters, &GAMSession, &GAMErrors)
    &HttpClient.AddHeader(!'Authorization', !'OAuth ' + &AccessToken.access_token)
    
    &RequestBody = '{  "inParam": ' + &TestCaseData.inParam.ToString() + '}'
    
    &HttpClient.AddString(&RequestBody)
    
    /* Act...                       */
    &HttpClient.Execute(!"POST", RestDemoProcedure.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
    &GAMApplication = GAMApplication.Get()
    &LoginAdditionalParameters.AuthenticationTypeName = !"local"
    &OAuthAdditionalParameters.ClientId = &GAMApplication.ClientId
    &OAuthAdditionalParameters.ClientSecret = &GAMApplication.ClientSecret
    &OAuthAdditionalParameters.Scope = !"FullControl"
EndSub

Availability

This feature  is available since GeneXus 17 upgrade 11.


Last update: November 2024 | © GeneXus. All rights reserved. GeneXus Powered by Globant