0

I'm trying to do some basic authorization by using the javascript function (basic-auth.js) here to encode:

function fn(creds) {
var temp = creds.USER_NAME + ':' + creds.PASSWORD;
var Base64 = Java.type('java.util.Base64');
var encoded = Base64.getEncoder().encodeToString(temp.toString().getBytes());
return 'Basic ' + encoded;
}

Here is my authorization feature file:

@tests=auth
Feature: Auth Management
Background:
    * url baseUrl
@test=accessToken
Scenario: generate access token
    Given path '/oauth2' + '/token'
    And header Authorization = call read('basic-auth.js') { username: '#(USER_NAME)', password: '#(PASSWORD)' }
    And form field grant_type = 'client_credentials'
    When method POST
    Then status 200
    * def accessToken = response.access_token

I'm trying to call in the username and password from a stored .yml (karate-config-stg.yml) using the format of #(USER_NAME) isn't pulling in the variable from the config file even through from that same config I'm getting the baseUrl without issue. Any help on this one is very appreciated! this is a new concept I'm trying to learn.

karate-config-stg.yml all fields are deleted for this post, in my local they have the appropriate values for staging:

baseUrl: ''
USER_NAME: ''
PASSWORD: ''
1
  • At first glance - I would think of the difference between baseUrl & USER_NAME is 'underscore'. As its working for baseUrl, Can you give a try without underscore ? Commented Jan 19, 2023 at 11:22

1 Answer 1

0

I found out why it wasn't working properly, the issue was in my js function - instead of using the variable name of creds.USER_NAME and creds.PASSWORD I just needed to change the names to creds.username and creds.password in line 2. In my header authorization I set the variables for those 2 fields with the information from my .yml file.

1
  • I suggested the same n comment though! :) Commented Jan 21, 2023 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.