I have a MySQL database and am trying to get this byte array using JDBC Request.
I am saving the result in a variable and then in a BeanShell encoding this result in a string and sending it through an HTTP POST:
BeanShell:
import java.net.URLEncoder;
import org.apache.commons.net.util.Base64;
String source = vars.get("storedToken_1");
byte[] encodedBytes = Base64.encodeBase64(source.getBytes());
String encoded = new String(encodedBytes);
String code = URLEncoder.encode(encoded, "UTF-8");
vars.put("reset_pass_token", code);
The thing is that this storedToken I'm getting from the db is coming corrupted so I'm not being able to authenticate the test.
Is there any other way I can retrieve this byte array value from the database and store it in a variable so I can send it in a POST?
If the variable comes as byte array, you need to handle it a little bit differently in Beanshell using vars.getObject() method, like:
String source = new String(vars.getObject("storedToken_1"));
Also the way you extract the variable may matter, JMeter won't "corrupt" the data, it might be the case that you misuse JDBC Test Element. See Debugging JDBC Sampler Results in JMeter article for available options listed and if it doesn't help - update your question with screenshot or detailed description of your JDBC element configuration.
Related
I already configured the token but I am trying to have a csv file for the login credentials. the login credentials generate code
Try BeanShell processor script to create a base 64 enoded char array and pass that as login credentials.
import org.apache.commons.codec.binary.Base64;
log.info(“Base64 Encoding ClientID & ClientPassword”);
//Create the complete string which needs to be encoded
String forEncoding = vars.get(“ClientID”) + “:” + vars.get(“ClientPassword”);
log.info(“forEncoding: “ + forEncoding);
//Encode the string
byte[] encoded_ClientCredentials = Base64.encodeBase64(forEncoding.getBytes());
//Save the encoded byte array into a String Variable
vars.put(“encoded_ClientCredentials”, new String(encoded_ClientCredentials));
log.info(“encoded_ClientCredentials: “ + vars.get(“encoded_ClientCredentials”));
If you need to provide Base64-encoded credentials you could use __base64Encode() function which is available via Custom JMeter Functions bundle which can be installed using JMeter Plugins Manager
See How to Use the Custom JMeter Functions Plugin article for more details.
From your question it's not really clear how the credentials should be passed, if it's about Basic Access Control it might be much easier to use HTTP Authorization Manager
I’ve got a call to my database working as a SQL select statement. But I am working to call a stored procedure using JMeter for further testing. I’m strictly working off of the JMX files and do not have JMETER integrated into our main Java project at this time.
I’ve setup the JMETER GUI with the JDBC Connection Configuration and the JDBC Request. I’ve made a successful call to my database with my callable statement with my string INPUT and get the string OUTPUT parameter string.
The OUTPUT parameter string only contains information about the call (user,system, success, etc…), but none of the values/data from the table -- which are found in the ResultSet/MetaData. But I cannot figure out how to get the ResultSet or the Metadata using the JDBC Request in JMETER.
In Java, I know I use the statement and just call statement.getResultSet() and perform a loop while resultSet.next() exists. How do I do this in JMETER?
I've tried adding an additional out parameter but then my statement rejects the call, because there is only one in-parameter. I've tried a variety of JMeter Assertions - but because the main call is only returning the out parameter, I cannot grab additional data.
Query: call XXXXX.readUser(?)
Parameter Values: ${inputJSONString}
Parameter Types: INOUT VARCHAR
Variable Names: ouputJSONString
Result Variable Name: ouputJSONString
View Results Tree: Response code: 200, Response message: OK, Output variables by position: Contains the whole JSON out parameter string with user, system, and success. Returns the table column headers but no values.
I do not have errors - the call is being made successfully. I just cannot figure out how to access the Result Set from JMETER.
Don't use the same reference name for the Variable Names and the Result Variable Name as the latter one will be overwritten.
So
Change ouputJSONString to i.e. ouputJSONStringObject
Add JSR223 PostProcessor as the child of the request
You will be able to access the JMeter's representation of the ResultSet as vars.getObject('ouputJSONStringObject') (basically ArrayList of HashMaps
See Debugging JDBC Sampler Results in JMeter article for more details.
Unfortunately you cannot access the normal ResultSet as it is not exposed anywhere and being converted via private function
I've just started out trying to write some tests in jmeter. It's the first time I've used it in earnest, and I'm struggling a bit with it.
I am running a test that searches for data on a website.
The returned data is in the format baseURL/customerID=
Now, the customer number depends on the customer being searched for and it is in base64.
I'm struggling to see how I can get the url updated.
I can see in the request that the field is parameter, and I know what the value is, as I have it stored in a csv file as CustomerID.
How can I convert that csv data to the url parameter?
I'm trying to use beanshell at the moment with this:
import org.apache.commons.codec.binary.Base64;
String customerID = vars.get("customerID");
String customerStringEncoded = Bas64Encoder.encode(customerID);
vars.put("customerStringEncoded",customerStringEncoded);
But it seems that it can't locate customerID.
I am supplying it wiht ${CustomerID} in the preprossor menu option, but I don't know what is wrong.
the error is "Typed variable declaration : Attempt to resolve method: encode() on undefined variable or class name: Bas64Encoder
"
Any tips?
Thanks
tgb
If the data is being stored in csv, you can use the "CSV Data Set config" to read the CSV File. CSVhttp://jmeter.apache.org/usermanual/component_reference.html#CSV_Data_Set_Config
I am load testing an API using Jmeter. The Header of the request has an authentication request which needs me to Base64 the url+Nonce+Unix timestamp and SHA256 the resultant value with a secret key.
The above needs to be passed in header along with Nonce and timestamp.
For the above scenario should I create a custom function or use any preprocessor ?
You can do it via Beanshell PreProcessor as follows:
Add a HTTP Header Manager as a child of your HTTP Request sampler
Add aforementioned Beanshell PreProcessor the same way
Put the following code into the PreProcessor's "Script" area:
import org.apache.commons.httpclient.auth.DigestScheme; // necessary imports
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.jmeter.protocol.http.control.Header;
String url = sampler.getUrl().toString(); // get URL
String nonce = DigestScheme.createCnonce(); // get nonce
long timestamp = System.currentTimeMillis() / 1000L;
String combined = url + nonce + timestamp; // put everything together
byte[] base64 = Base64.encodeBase64(combined.getBytes()); // encode as Base64
String headerValue = DigestUtils.sha256Hex(base64); // encode SHA256
sampler.getHeaderManager().add(new Header("headerName", headerValue)); // add generated header to request
sampler here is a shorthand reference to parent HTTP Request Sampler class which I believe is HTTPSamplerProxy so its methods are used to get URL and add generated header value.
methods to generate MD5 hash and SHA256 hex are from Apache Commons libraries which are widely used under JMeter's hood.
See How to use BeanShell: JMeter's favorite built-in component guide for more information on using Beanshell scripting in JMeter tests.
Your best bet is to use a BSF Pre-Processor in JavaScript mode to do everything the client normally would. You'll have to take the client JS and modify it to work without FORM data.
You can build the entire header in JS exactly like a client would. BSF Pre-Processor allows you to access jmeter run-time variables, so you would create a new one to store the SHA256 hash value, and use that in a HTTP Header Manager of the sample that needs the authorization.
-Addled
Downloaded eclipse.
Wrote a custom jmeter package.
Exported it as a .jar from eclipse to jmeter lib/ext folder.
Called the package function in beanshell sampler
Thanks for your answers
#dmitrit's answer is helpful but I needed to do some tweaks to the code to get it to work. Here is what I did:
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
String apiKey = vars.get("ApiKey");
String apiSecret = vars.get("ApiSecret");
long timestamp = System.currentTimeMillis() / 1000L;
String combined = apiKey + apiSecret + timestamp;
String generatedSignature = DigestUtils.sha256Hex(combined);
vars.put("GeneratedSignature", generatedSignature);
Note the main differences are:
Most important: DigestUtils.sha256Hex takes a String instead of a byte array. Converting first to bytes screwed up the hash, I think due to padding.
I added the resulting value to vars so it can be used later in Jmeter in the usual way (${GeneratedSignature}).
ApiKey and ApiSecret are defined elsewhere in a Jmeter User Defined Variables element.
With this I was able to make the following work with Mashery in accordance with their authentication instructions posted here.
I want to call a jdbc function through Jmeter and I am able to do it with simple function that takes text parameters but the function is slightly changed now and it takes parameter as bytea array of image.
Please suggest how to browse an image from my PC local directory and how can I convert that image to byte array and pass that array to Jmeter jdbc call.
I think that you should be able to use option 3 of How to Send Byte Array in http request in Jmeter solution, add a Beanshell PreProcessor as a child of your JDBC Request with code like
import org.apache.commons.compress.utils.IOUtils;
FileInputStream in = new FileInputStream("/path/to/your/image.jpg");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IOUtils.copy(in,bos);
byte[] data = bos.toByteArray();
vars.put("image", new String(data));
and refer image bytes as ${image} in the JDBC Request Sampler.
For advanced information on Beanshell scripting see How to use BeanShell: JMeter's favorite built-in component guide.