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

Redirect endpoint url is not displaying in Serenity Report #2037

Closed
Krithikmano opened this issue Apr 19, 2020 · 3 comments
Closed

Redirect endpoint url is not displaying in Serenity Report #2037

Krithikmano opened this issue Apr 19, 2020 · 3 comments

Comments

@Krithikmano
Copy link

Serenithy version 2.0.83

for generate token i need to use redirecturl. i use the below code for generate token. But in report it is displaying only the base url not showing the redirect url and other information

Response response =
given()
.header("Authorization",:)
.contentType("application/x-www-form-urlencoded")
.params("grant_type","authorization_code") .params("redirect_uri",REDIRECT_URL)
.params("response_type","code")
.params("code", AUTHORIZATION_CODE)
.auth()
.preemptive()
.basic(CLIENT_ID, CLIENT_SECRET)
.when()
.post(BASE_URI+"/oauth2/token");

@wakaleo
Copy link
Member

wakaleo commented Apr 20, 2020

I’m not sure this is possible - Serenity simply reports what you provide to RestAssured, not what happens when you send the request over the network. Could you add a screenshot of the reports to show what you mean?

@eitzenbe
Copy link
Contributor

eitzenbe commented Dec 4, 2020

I THINK he relates to something i just stumbled upon today too:

When you do a GET request you see above the REST Query section all details, complete URL of the request (for testing / reproducing an failure,...)

But with POST you see just the basic URL but no information about which form params have been sent (please note that this applies only to url encoded from submissions, where the data is in params(), if you place it in body for plain POJO/JSON calls its displayed....

So its probably a feature request? But one that would make really sense! ;)

@eitzenbe
Copy link
Contributor

eitzenbe commented Dec 4, 2020

@wakaleo BTW i did a small utility to add the requests as cURL commands. Its PoC but might be something to consider to add to mainline as it would make reproducing bugs really really easy ;)

The reporter is alpha and by far not feature complete....

How to use

// add filter to retrieve rest assured logging
final ByteArrayOutputStream reqDetails = new ByteArrayOutputStream();
reqSpec.filter(new RequestLoggingFilter(LogDetail.ALL, true, new PrintStream(reqDetails), true));
// run restAssured request 
final Response r = reqSpec.request(method, uri).thenReturn();
// parse logging data and create cURL command
cURLReporter.addcURLCommand(new String(reqDetails.toByteArray(), StandardCharsets.UTF_8));

cURLReporter class

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import net.serenitybdd.core.Serenity;
import net.thucydides.core.steps.StepEventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class cURLReporter {

    static Logger l = LoggerFactory.getLogger("curl");

    public static void addcURLCommand(final String rALogDetails) {
        final String[] lines = rALogDetails.split("\\n");
        final String uri = Stream.of(lines)
            .filter(l -> l.trim().startsWith("Request URI:"))
            .map(cURLReporter::getValue)
            .findFirst().orElse(null);
        final String method = Stream.of(lines)
            .filter(l -> l.trim().startsWith("Request method:"))
            .map(cURLReporter::getValue)
            .findFirst().orElse(null);

        final StringBuilder curlCmd = new StringBuilder("curl ");
        // add headers
        final List<String> headers = getValues(lines, "Headers");
        for (final String header : headers) {
            final int equal = header.indexOf("=");
            curlCmd.append("-H \"").append(header.substring(0, equal))
                .append(": ").append(header.substring(equal + 1)).append("\" ");
        }

        if (uri != null && method != null) {
            boolean first = true;
            final List<String> params;

            switch (method) {
                case "GET":
                    curlCmd.append("-X GET \"").append(uri).append("\" ");
                    break;
                case "POST":
                    // add form params
                    params = getValues(lines, "Form params");
                    for (final String param : params) {
                        final int equal = param.indexOf("=");
                        if (first) {
                            first = false;
                            curlCmd.append("--data \"");
                        } else {
                            curlCmd.append("&");
                        }
                        curlCmd.append(param.substring(0, equal))
                            .append("=").append(param.substring(equal + 1));
                    }
                    if (!first) {
                        curlCmd.append("\" ");
                    }
                    curlCmd.append("-X POST \"").append(uri).append("\" ");
                    break;
            }
        } else {
            curlCmd.append("Unable to parse log data");
        }
        curlCmd.append("\n\n").append(rALogDetails);
        if (StepEventBus.getEventBus().isBaseStepListenerRegistered()) {
            Serenity.recordReportData().withTitle("cURL").andContents(curlCmd.toString());
        }
        l.info("cURL command: " + curlCmd);
    }

    private static String getValue(final String line) {
        final int start = line.lastIndexOf("\t");
        return line.substring(start).trim();
    }

    private static List<String> getValues(final String[] lines, final String blockToken) {
        final List<String> values = new ArrayList<>();
        boolean blockStarted = false;
        for (final String line : lines) {
            if (!blockStarted && line.startsWith(blockToken + ":")) {
                blockStarted = true;
                values.add(getValue(line));
            } else if (blockStarted) {
                final int tab = line.indexOf("\t");
                final int colon = line.indexOf(":");
                if (colon != -1 && colon < tab) {
                    // next block starts
                    return values;
                } else {
                    // add value
                    values.add(getValue(line));
                }
            }
        }
        return values;
    }
}

@wakaleo wakaleo closed this as completed Feb 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants