Ukuqinisekiswa kwe-OAuth 2.0 ngokuGatling usebenzisa iTywina yeBhere

Esi sithuba sicacisa indlela yokwenza uqinisekiso lwe-Oauth2 ngeGatling.

Kulo mzekelo, sithumela isicelo sokwenza umsebenzisi. Nangona kunjalo, indawo yokuphela yomsebenzisi ikhuselwe kwaye ifuna ukufikelela kwi-tag.

Kuqala, siyakufumana i-bearer_token okanye ukufikelela-uphawu kwaye siyithumele njengesihloko kwisicelo esilandelayo se-API sokwenza umsebenzisi.


Ukucacisa oku, siya kuba sisebenzisa isakhiwo seprojekthi enye yeGatling esakhe ngaphambili:

Isakhelo soVavanyo lweNtsebenzo kunye neGatling neMaven


Xa silandela la manyathelo kule post ingentla, siza kuba nesakhiwo seprojekthi ngolu hlobo lulandelayo:



Ukuchaza iiparameter kuBumbeko

Kuqala sichaza iiparameter zethu ze-OAuth 2.0 kwi | _ _ _ _ | into phantsi kwe qwalasela ifolda:

Configuration.scalaPhawula:Ngokwesiqhelo, okusingqongileyo, iklayenti_id kunye nomxhasi_iimfihlo zithunyelwa ngaphandle kumatshini iimvavanyo ziya kuba ziqhutywa, ukuze sikwazi ukusebenzisa Inkqubo.getProperty () ukufunda amaxabiso.

Izicelo

Ngoku kufuneka sibhale ikhowudi ethumela isicelo kwiserver yesigunyaziso ukufumana ithokheni ethweleyo.


Isicelo se-OAuth 2.0-ukufikelela_umqondiso

Le fayile object Configuration { val environment: String = System.getProperty('environment') val clientId: String = System.getProperty('CLIENT_ID') val clientSecret: String = System.getProperty('CLIENT_SECRET') val apiURL: String = 'https://some-sub-domain.' + environment + 'some-domain.com/api' var tokenPath: String = 'https://some-sub-domain' + environment + '.eu.auth0.com/oauth/token' val userPath = '/identity/iaa/v1/users' } igcinwa phantsi kwe izicelo ulawulo kulwakhiwo lwethu lweprojekthi.

AuthRequest.scala

Kule khowudi ingentla, sigcina ukufikelela_umqondiso kwifayile.

Le fowuni ingentla, ifumana nje ukufikelela-uphawu.

Sifuna esinye isicelo sokwenza umsebenzisi ngokuthumela ukufikelela_thokheni njengesihloko.


Isicelo soMsebenzisi

Isicelo sethu somsebenzisi sikwifayile ebizwa import java.io.{BufferedWriter, FileWriter} import config.Configuration import io.gatling.core.Predef._ import io.gatling.http.Predef._ object AuthRequest { val getAccessToken = exec(http('Get access token')
.post(Configuration.tokenPath)
.body(StringBody(
s'''{

'client_id': '${Configuration.clientId}',

'client_secret': '${Configuration.clientSecret}',

'audience': 'https://some-domain-name.com/user',

'grant_type': 'client_credentials',

'scope': 'user:admin'
}'''
))
.asJson
.headers(Map('Content-Type' -> 'application/json'))
.check(status.is(200))
.check(jsonPath('$.access_token').saveAs('access_token')))
.exec {
session =>
val fw = new BufferedWriter(new FileWriter('access_token.txt', true))
try {

fw.write(session('access_token').as[String] + ' ')
}
finally fw.close()
session
} }
kwaye igcinwa phantsi kwe izicelo ifolda.

UserRequests.scala

Imeko

Ngoku sibhala into yento. Kulo mzekelo into yethu ibizwa import config.Configuration.{apiURL, userPath} import io.gatling.core.Predef._ import io.gatling.http.Predef._ object UserRequests { private val auth0Headers = Map(
'Accept' -> 'application/json, text/javascript, */*; q=0.01',
'Content-Type' -> 'application/json',
'Authorization' -> 'Bearer ${access_token}') val createUser = exec(http('Create user')
.post(apiURL + userPath)
.headers(auth0Headers)
.body(ElFileBody('createUser.json'))
.check(status.is(201))) }
kwaye igcinwa phantsi kwe imeko ifolda.

UserScenarios.scala

Isicelo esingentla, sithumela isicelo se-POST ukudala umsebenzisi nge-access_token njengomthwali kwintloko.



Ukulinganisa

Okokugqibela ifayile yethu yokulinganisa ibizwa import requests.{AuthRequest, UserRequests} import io.gatling.core.Predef._ object UserScenarios { var userIds:Array[Map[String,String]] =
(100 to 900).toArray map ( x => { Map( 'userId' -> x.toString) }) val getAccessToken = scenario('Get token')
.exec(AuthRequest.getAccessToken) val createUserScenario = scenario('Create user')
.feed(userIds.circular)
.exec(UserAuthZRequest.getAccessToken)
.exec(UserRequests.createUser) }
igcinwa phantsi kwe ukulinganisa ifolda.


UserSimulation.scala

Ukuqhuba iimvavanyo esizisebenzisayo

import scenario.UserScenarios import io.gatling.core.Predef._ import scala.concurrent.duration._ class UserSimulation extends Simulation { setUp(
UserScenarios.createUserScenario.inject(rampUsers(250) during (15 minutes)), ) }