OAuth 2.0 Kusimbiswa NeGatling Uchishandisa Mutakuri Chiratidzo

Iyi posvo inotsanangura maitiro ekuita Oauth2 Kusimbiswa neGatling.

Mumuenzaniso uyu, tiri kutumira chikumbiro chekugadzira mushandisi. Nekudaro, iyo yekupedzisira endpoint yekushandisa inodzivirirwa uye inoda access_token.

Kutanga, isu tinowana inotakura_chiratidzo kana yekuwanira_chiratidzo uye wozoitumira semusoro kune inotevera API chikumbiro chekugadzira mushandisi.

Kuenzanisira izvi, isu tichave tichishandisa imwecheteyo chimiro chechirongwa cheGatling chatakavaka kare:

Performance Kuedza Sisitimu neGatling neMaven

Kana isu tikatevera nhanho mune iri pamusoro posvo, isu tichava nechirongwa chedu chirongwa seinotevera:



Kutsanangura maParamende muKugadziridza

Kutanga isu tinotsanangura edu OAuth 2.0 parameter mu Configuration.scala Chinhu chefaira pasi pe config dhairekitori:

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' } Ongorora:Kazhinji, nharaunda, client_id uye client_secrets zvinotumirwa kunze muchina bvunzo dzichange dzichimhanya, saka tinogona kushandisa iyo System.getProperty () kuverenga tsika.

Zvikumbiro

Iye zvino tinoda kunyora kodhi iyo inotumira chikumbiro kuseva yemvumo kuti tiwane mutakuri chiratidzo.

OAuth 2.0 Chikumbiro - access_token

Iyi faira AuthRequest.scala inoponeswa pasi pe zvikumbiro dhairekitori mune yedu dhizaini dhizaini.

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
} }

Mune ino yepamusoro kodhi snippet, isu tiri zvakare kuchengetedza iyo yekuisa_chiratidzo kune faira.

Kufona kuri pamusoro, kunongowana yekuwana_chiratidzo.

Tinoda chimwe chikumbiro chekugadzira mushandisi nekutumira iyo yekuwana_chiratidzo semusoro.

Mushandisi Chikumbiro

Yedu yekukumbira mushandisi iri mune faira inonzi UserRequests.scala uye tinoponeswa pasi pe zvikumbiro forodha.

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))) }

Muenzaniso

Iye zvino tinonyora mamiriro ezvinhu. Mumuenzaniso uyu chinhu chedu chinonzi UserScenarios.scala uye tinoponeswa pasi pe mamiriro forodha.

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) }

Ichi chikumbiro chiri pamusoro, chinotumira iyo POST chikumbiro kuti igadzire mushandisi iine access_token semutakuri mumusoro.

Kufananidza

Pakupedzisira yedu yekufananidza faira inonzi UserSimulation.scala inoponeswa pasi pe mifananidzo forodha.

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)), ) }

Kumhanyisa bvunzo dzatinoshandisa

mvn clean gatling:test