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

Can we pass Authentication in header? #15

Open
sri102info opened this issue Apr 24, 2017 · 8 comments
Open

Can we pass Authentication in header? #15

sri102info opened this issue Apr 24, 2017 · 8 comments

Comments

@sri102info
Copy link

We need to test our API end point which uses Authentication in header, whereas the tester function will take url, server, method and contenttype.

Can we have Authentication in tester function?

@pramodj009
Copy link

I also have same problem, used django.auth for authentication of users in App and I want to test Graphql from client side(used Apollo Client) with Authentication header.

@msmfsd
Copy link

msmfsd commented Apr 13, 2018

Yes:

const {tester} = require('graphql-tester')
const myAuthToken = 'HFJGHGFJHFGJ456456456456'

const test = tester({
  url: 'http://localhost:3000/graphql',
  contentType: 'application/json',
  authorization: myAuthToken,
})

@sjakati98
Copy link

sjakati98 commented Jul 12, 2018

Having a little trouble with this actually. My header has a hypen in it, and so I tried something along the lines of

var auth_header = {}
auth_header["hypen-header"] = "somevalue";
var test = tester({
   url: ...
   ...auth_header
})

And that does not seem to be working. Any workarounds you can think of, @msmfsd ?

@ghost
Copy link

ghost commented Sep 7, 2018

@sjakati98 Did you get this to work? Or how did you do?

@msmfsd
Copy link

msmfsd commented Sep 12, 2018

@Ingelhag @sjakati98 I got it working - see my comment above ^ - make sure you quit your dev server before running tests

@msmfsd
Copy link

msmfsd commented Sep 12, 2018

@sjakati98 did you try camel case?

@nsourov
Copy link

nsourov commented May 18, 2019

How I use same-origin. As I'm in my playground I'm setting this "request.credentials": "same-origin" and that set the cookie in the same origin. But while testing how I set this?

@ashleypatricks
Copy link

ashleypatricks commented Nov 17, 2020

Hi Guys,

For anyone who stumbled on this problem, and especially for @msmfsd @sri102info @pramodj009 @nsourov, if you need to pass in an authorization header then you could probably do it as @msmfsd mentioned, however it did not actually work in my case.

Instead to pass in ANY custom header you should actually install this package graphql-tester-options. Instead of installing graphql-tester. You can now send in the graphql query as the 1st argument, and then as the second argument just send in an object defining your custom header. Job done. Here is an example where I send in the custom header x-mock:

// Import library
import { tester } from 'graphql-tester-options';

// Define your tester object
const testGql = tester({
  url: 'your_graphql_endpoint',
  contentType: 'application/json',
});

// Run your test (I used Jest)
  test('Your solution test!', async () => {
    const {
      data: {
        processCustomerTransactionConsent: { status },
      },
    } = await testGql(
      JSON.stringify({
        query: `mutation processCustomerTransactionConsent($input: TransactionConsentInput!) {
          processCustomerTransactionConsent(input: $input){
            status
            transactionStatus
          }
         }`,
        variables: {
          input: {
            id: 'test_id',
            customerDecision: 'SUCCESS',
            customerId: 'test_customer_id',
            debitAccount: 'test_debit_account',
            channel: 'test_channel',
          },
        },
      }),
      {
        headers: {
          'x-mock': 'true',
        },
      },
    );

Cheers!
Ash.

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

6 participants