Last active
January 29, 2020 12:20
-
-
Save barancev/2264115494461d8b929f16a99012a3a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ru.stqa.pft.mantis.appmanager; | |
| import org.apache.http.NameValuePair; | |
| import org.apache.http.client.entity.UrlEncodedFormEntity; | |
| import org.apache.http.client.methods.CloseableHttpResponse; | |
| import org.apache.http.client.methods.HttpGet; | |
| import org.apache.http.client.methods.HttpPost; | |
| import org.apache.http.impl.client.CloseableHttpClient; | |
| import org.apache.http.impl.client.HttpClients; | |
| import org.apache.http.impl.client.LaxRedirectStrategy; | |
| import org.apache.http.message.BasicNameValuePair; | |
| import org.apache.http.util.EntityUtils; | |
| import java.io.IOException; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class HttpSession { | |
| private CloseableHttpClient httpclient; | |
| private ApplicationManager app; | |
| public HttpSession(ApplicationManager app) { | |
| this.app = app; | |
| httpclient = HttpClients.custom().setRedirectStrategy(new LaxRedirectStrategy()).build(); | |
| } | |
| public boolean login(String username, String password) throws IOException { | |
| HttpPost post = new HttpPost(app.getProperty("web.baseUrl") + "/login.php"); | |
| List<NameValuePair> params = new ArrayList<>(); | |
| params.add(new BasicNameValuePair("username", username)); | |
| params.add(new BasicNameValuePair("password", password)); | |
| params.add(new BasicNameValuePair("secure_session", "on")); | |
| params.add(new BasicNameValuePair("return", "index.php")); | |
| post.setEntity(new UrlEncodedFormEntity(params)); | |
| CloseableHttpResponse response = httpclient.execute(post); | |
| String body = geTextFrom(response); | |
| return body.contains(String.format("<span class=\"user-info\">%s</span>", username)); | |
| } | |
| private String geTextFrom(CloseableHttpResponse response) throws IOException { | |
| try { | |
| return EntityUtils.toString(response.getEntity()); | |
| } finally { | |
| response.close(); | |
| } | |
| } | |
| public boolean isLoggedInAs(String username) throws IOException { | |
| HttpGet get = new HttpGet(app.getProperty("web.baseUrl") + "/index.php"); | |
| CloseableHttpResponse response = httpclient.execute(get); | |
| String body = geTextFrom(response); | |
| return body.contains(String.format("<span class=\"user-info\">%s</span>", username)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment