Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Describe how to change an URL in the onRequest method of the RequestHook #7084

Open
Aleksey28 opened this issue Jun 13, 2022 · 2 comments
Labels
AREA: docs An issue related to the product documentation. TYPE: enhancement The accepted proposal for future implementation.

Comments

@Aleksey28
Copy link
Collaborator

What is your Scenario?

There isn't information about how to change an URL of the request in the RequestHook,

What are you suggesting?

There should be informed on how to change a request URL. It needs to change hostname, host, port, protocol and path of the e.requestOptions on values that are required for a new resource. Also, We should change the request headers depending on the new resource. Option url is read-only, which means that trying to change it doesn't alter the real url.

For example:

class CustomRequestHook extends RequestHook {
    onRequest (e) {
        e.requestOptions.protocol        = 'https:';
        e.requestOptions.host            = 'devexpress.github.io';
        e.requestOptions.hostname        = 'devexpress.github.io';
        e.requestOptions.port            = '';
        e.requestOptions.path            = '/testcafe/example/thank-you.html';
        e.requestOptions.headers['host'] = 'devexpress.github.io';
    }

    onResponse (responseEvent) {
    }
}

What alternatives have you considered?

No response

Additional context

No response

@Aleksey28 Aleksey28 added the TYPE: enhancement The accepted proposal for future implementation. label Jun 13, 2022
@AndreyBelym AndreyBelym added the AREA: docs An issue related to the product documentation. label Jul 7, 2022
@pgusilic-devops
Copy link

pgusilic-devops commented Dec 2, 2024

@Aleksey28 do you need help? Here an example:

import { RequestMock } from "testcafe";
const createProtocolSuccess = (bodyContent: Record<string, any>) => RequestMock()
    .onRequestTo({ url: 'YOURURLGOESHERE', method: 'GET' })
    .respond((req, res) => {
        res.headers['access-control-allow-origin'] = '*';
        res.headers['access-control-allow-headers'] =
            'expiry,uid,client,access-token,token-type,content-type';
        res.headers['access-control-allow-credentials'] = 'false';
        res.setBody(bodyContent);
        res.statusCode = 200
    })
fixture("TEST")
    .page(baseUrl)
    .beforeEach(async (t: TestController) => {
        await loginPage.login({
            userName: login,
            password: password,
        });
        await t.maximizeWindow();
        await t.wait(1000);
    })
test.meta({ CID: "01", type: TestType.Critical }).timeouts({
    pageLoadTimeout: 5000,
})
    .requestHooks(createProtocolSuccess(
        createDetailedContent({
            "casePermissions": {
                "canRequestCVR": true,
            }
        })
    ("Assert Requests Exist", async (t: TestController) => {
        await t.navigateTo("/MYCASE/");
        await t.wait(3000)
        await detailedView.assertion.assertStatus(true)
        await detailedView.assertion.assertButtonStatus(true)
    });

@testcafe-need-response-bot testcafe-need-response-bot bot added the STATE: Need response An issue that requires a response or attention from the team. label Dec 2, 2024
@Aleksey28
Copy link
Collaborator Author

Hi @pgusilic-devops,

Thank you for your time. However, this is not a request for help. It's a suggestion to add information to the documentation.

@testcafe-need-response-bot testcafe-need-response-bot bot removed the STATE: Need response An issue that requires a response or attention from the team. label Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AREA: docs An issue related to the product documentation. TYPE: enhancement The accepted proposal for future implementation.
Projects
None yet
Development

No branches or pull requests

4 participants