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

feat: Support to generator JavaScript code from a HTTP testCase. #400

Merged
merged 2 commits into from
Apr 26, 2024

Conversation

YukiCoco
Copy link
Contributor

add javascript generator.
add javascript code generator test data.

What type of PR is this?
feat: Support to generator JavaScript code from a HTTP testCase.
Which issue(s) this PR fixes:
Fixes #395

@CLAassistant
Copy link

CLAassistant commented Apr 25, 2024

CLA assistant check
All committers have signed the CLA.

@LinuxSuRen LinuxSuRen added the enhancement New feature or request label Apr 25, 2024
Copy link
Owner

@LinuxSuRen LinuxSuRen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found an error when try it in the Chrome console.

image

@YukiCoco
Copy link
Contributor Author

YukiCoco commented Apr 25, 2024

I found an error when try it in the Chrome console.

image

In browsers or Node.js, sending an HTTP GET request with a body is not allowed, and it will be blocked by the browser. However, I've still generated code for this situation. When using a GET request in this manner, I've added a comment: // WARNING: JavaScript does not allow a body to be sent with GET requests. This is to prompt the user to switch to a different request method.

Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET

Update: On browsers, when XMLHttpRequest sends a GET request with a body, the body is ignored, while using Fetch throws an exception, making it infeasible to send GET requests with body in browsers. In NodeJS, using Fetch still throws an exception, but using axios is okay (this still doesn't work in browsers because it encapsulates XMLHttpRequest). Perhaps I could write another axios example template, similar to what Postman offers? I wonder if there is a more user-friendly solution.

Reference: https://xhr.spec.whatwg.org/#the-send()-method

@LinuxSuRen
Copy link
Owner

I've added a comment: // WARNING: JavaScript does not allow a body to be sent with GET requests. This is to prompt the user to switch to a different request method.

This is ok to me. How about adding comment in line 24 and 25? So, those source code can work well. Even we can add a console output, for example:

console.log('the body is ignored because this is a GET request')

@YukiCoco
Copy link
Contributor Author

YukiCoco commented Apr 25, 2024 via email

Copy link

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@YukiCoco
Copy link
Contributor Author

Now, the new code generator looks like this:

async function makeRequest() {
    const headers = new Headers();
    headers.append("User-Agent", "atest");
    const cookieHeader = [
        "name=value",
    ].join("; ");
    headers.append("Cookie", cookieHeader);

    const requestOptions = {
        method: "GET",
        headers: headers,
        redirect: "follow"
    };
    console.log('the body is ignored because this is a GET request')
    /*
    let body = `{"key": "value"}`;
    requestOptions.body = body;
    */

    try {
        const response = await fetch("https://www.baidu.com", requestOptions);
        if (response.ok) { // Check if HTTP status code is 200/OK
            const text = await response.text();
            console.log(text);
        } else {
            throw new Error('Network response was not ok. Status code: ' + response.status);
        }
    } catch (error) {
        console.error('Fetch error:', error);
    }
}

makeRequest();

Copy link
Owner

@LinuxSuRen LinuxSuRen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested it manually. It works well. Thanks for your effort. And SO HAPPY to have as the 18th contributor.

This project will be better because of you.

image

@LinuxSuRen LinuxSuRen merged commit 6f2fed9 into LinuxSuRen:master Apr 26, 2024
10 checks passed
@LinuxSuRen LinuxSuRen added the ospp 开源之夏 https://summer-ospp.ac.cn/ label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ospp 开源之夏 https://summer-ospp.ac.cn/
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support to generator JavaScript code from a HTTP testCase
3 participants