Score:1

How to create functional tests for testing REST requests that the site has?

jp flag

I'm trying to understand how to create tests for REST Endpoint that a custom module creates.

I managed to create a Unit Test for the Get method of the Rest-resource and now I want to create a Functional test for the REST-endpoint.

The idea of the test is the following. The test should create a user account with required roles and permissions, authenticate it on the site and send a GET request on behalf of it. And after that, the test should check up the content of the response in JSON format and compare it with the expected.

I would be grateful if you could clarify the following questions:

  1. How to give anonymous users some permissions? I tried to do it via import of the user.role.anonymous.config with needed permissions into my test config table but without any success.
  2. How to send requests to the REST-endpoint correctly? As far as I'm concerned, the drupalGet method is not the best solution for that. I had an idea to use exec('curl ...') for that, but I haven't found a true entry point to the Drupal test environment, and moreover, I'm not sure that it's the correct way.
  3. How to perform a sequence of requests in this case? I mean requests for Oauth2/Token authentication and after that, requests to REST-endpoints.

Thanks in advance.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.