graphql-tester icon indicating copy to clipboard operation
graphql-tester copied to clipboard

Can we pass Authentication in header?

Open sri102info opened this issue 7 years ago • 8 comments

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?

sri102info avatar Apr 24 '17 09:04 sri102info

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.

pramodj009 avatar Jan 10 '18 12:01 pramodj009

Yes:

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

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

msmfsd avatar Apr 13 '18 04:04 msmfsd

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 ?

sjakati98 avatar Jul 12 '18 03:07 sjakati98

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

ghost avatar Sep 07 '18 09:09 ghost

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

msmfsd avatar Sep 12 '18 00:09 msmfsd

@sjakati98 did you try camel case?

msmfsd avatar Sep 12 '18 00:09 msmfsd

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?

nsourov avatar May 18 '19 07:05 nsourov

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.

ashleypatricks avatar Nov 17 '20 20:11 ashleypatricks