How to use WithHTTPClient method of client Package

Best K6 code snippet using client.WithHTTPClient

dcr32.go

Source:dcr32.go Github

copy

Full Screen

1package compliant2import (3 "fmt"4 "github.com/OpenBankingUK/conformance-dcr/pkg/compliant/step"5 "github.com/dgrijalva/jwt-go"6 "net/http"7 "time"8 "github.com/OpenBankingUK/conformance-dcr/pkg/compliant/auth"9 "github.com/OpenBankingUK/conformance-dcr/pkg/compliant/schema"10)11// nolint:lll12const (13 specLinkDiscovery = "https://openbanking.atlassian.net/wiki/spaces/DZ/pages/1078034771/Dynamic+Client+Registration+-+v3.2#DynamicClientRegistration-v3.2-Discovery"14 specLinkRegisterSoftware = "https://openbanking.atlassian.net/wiki/spaces/DZ/pages/1078034771/Dynamic+Client+Registration+-+v3.2#DynamicClientRegistration-v3.2-POST/register"15 specLinkDeleteSoftware = "https://openbanking.atlassian.net/wiki/spaces/DZ/pages/1078034771/Dynamic+Client+Registration+-+v3.2#DynamicClientRegistration-v3.2-DELETE/register/{ClientId}"16 specLinkRetrieveSoftware = "https://openbanking.atlassian.net/wiki/spaces/DZ/pages/1078034771/Dynamic+Client+Registration+-+v3.2#DynamicClientRegistration-v3.2-GET/register/{ClientId}"17 specLinkUpdateSoftware = "https://openbanking.atlassian.net/wiki/spaces/DZ/pages/1078034771/Dynamic+Client+Registration+-+v3.2#DynamicClientRegistration-v3.2-PUT/register/{ClientId}"18)19func NewDCR32(cfg DCR32Config) (Manifest, error) {20 secureClient := cfg.SecureClient21 authoriserBuilder := cfg.AuthoriserBuilder22 validator := cfg.SchemaValidator23 scenarios := Scenarios{24 DCR32ValidateOIDCConfigRegistrationURL(cfg),25 DCR32CreateSoftwareClient(cfg, secureClient, authoriserBuilder),26 DCR32DeleteSoftwareClient(cfg, secureClient, authoriserBuilder),27 DCR32CreateInvalidRegistrationRequest(cfg, secureClient, authoriserBuilder),28 DCR32RetrieveSoftwareClient(cfg, secureClient, authoriserBuilder, validator),29 DCR32RetrieveWithInvalidCredentials(cfg, secureClient, authoriserBuilder),30 DCR32UpdateSoftwareClient(cfg, secureClient, authoriserBuilder),31 DCR32UpdateSoftwareClientWithWrongId(cfg, secureClient, authoriserBuilder),32 DCR32RetrieveSoftwareClientWrongId(cfg, secureClient, authoriserBuilder),33 DCR32RegisterSoftwareWrongResponseType(cfg, secureClient, authoriserBuilder),34 }35 return NewManifest("DCR32", "1.0", scenarios)36}37func DCR32ValidateOIDCConfigRegistrationURL(cfg DCR32Config) Scenario {38 return NewBuilder(39 "DCR-001",40 "Validate OIDC Config Registration URL",41 specLinkDiscovery,42 ).TestCase(43 NewTestCaseBuilder("Validate Registration URL").44 ValidateRegistrationEndpoint(cfg.OpenIDConfig.RegistrationEndpoint).45 Build(),46 ).Build()47}48func DCR32CreateSoftwareClient(49 cfg DCR32Config,50 secureClient *http.Client,51 authoriserBuilder auth.AuthoriserBuilder,52) Scenario {53 return NewBuilder(54 "DCR-002",55 "Dynamically create a new software client",56 specLinkRegisterSoftware,57 ).58 TestCase(DCR32CreateSoftwareClientTestCases(cfg, secureClient, authoriserBuilder)...).59 TestCase(DCR32DeleteSoftwareClientTestCase(cfg, secureClient)).60 Build()61}62func DCR32CreateSoftwareClientTestCases(63 cfg DCR32Config,64 secureClient *http.Client,65 authoriserBuilder auth.AuthoriserBuilder,66) []TestCase {67 return []TestCase{68 NewTestCaseBuilder("Register software client").69 WithHttpClient(secureClient).70 GenerateSignedClaims(authoriserBuilder).71 PostClientRegister(cfg.OpenIDConfig.RegistrationEndpointAsString()).72 AssertStatusCodeCreated().73 ParseClientRegisterResponse(authoriserBuilder).74 Build(),75 NewTestCaseBuilder("Retrieve client credentials grant").76 WithHttpClient(secureClient).77 GetClientCredentialsGrant(cfg.OpenIDConfig.TokenEndpoint).78 Build(),79 }80}81func DCR32DeleteSoftwareClientTestCase(82 cfg DCR32Config,83 secureClient *http.Client,84) TestCase {85 name := "Delete software client"86 if !cfg.DeleteImplemented {87 return NewTestCase(88 fmt.Sprintf("(SKIP Delete endpoint not implemented) %s", name),89 []step.Step{},90 )91 }92 return NewTestCaseBuilder(name).93 WithHttpClient(secureClient).94 ClientDelete(cfg.OpenIDConfig.RegistrationEndpointAsString()).95 Build()96}97func DCR32DeleteSoftwareClient(98 cfg DCR32Config,99 secureClient *http.Client,100 authoriserBuilder auth.AuthoriserBuilder,101) Scenario {102 id := "DCR-003"103 name := "Delete software is supported"104 if !cfg.DeleteImplemented {105 return NewBuilder(106 id,107 fmt.Sprintf("(SKIP Delete endpoint not implemented) %s", name),108 specLinkDeleteSoftware,109 ).Build()110 }111 return NewBuilder(112 id,113 name,114 specLinkDeleteSoftware,115 ).116 TestCase(DCR32CreateSoftwareClientTestCases(cfg, secureClient, authoriserBuilder)...).117 TestCase(DCR32DeleteSoftwareClientTestCase(cfg, secureClient)).118 TestCase(119 NewTestCaseBuilder("Retrieve delete software client should fail").120 WithHttpClient(secureClient).121 ClientRetrieve(cfg.OpenIDConfig.RegistrationEndpointAsString()).122 AssertStatusCodeUnauthorized().123 Build(),124 ).Build()125}126func DCR32CreateInvalidRegistrationRequest(127 cfg DCR32Config,128 secureClient *http.Client,129 authoriserBuilder auth.AuthoriserBuilder,130) Scenario {131 return NewBuilder(132 "DCR-004",133 "Dynamically create a new software client will fail on invalid registration request",134 specLinkRegisterSoftware,135 ).136 TestCase(137 NewTestCaseBuilder("Register software client fails on expired claims").138 WithHttpClient(secureClient).139 GenerateSignedClaims(140 authoriserBuilder.141 WithJwtExpiration(-time.Hour),142 ).143 PostClientRegister(cfg.OpenIDConfig.RegistrationEndpointAsString()).144 AssertStatusCodeBadRequest().145 Build(),146 ).147 TestCase(148 NewTestCaseBuilder("Register software client fails on invalid issuer").149 WithHttpClient(secureClient).150 GenerateSignedClaims(151 authoriserBuilder.152 WithIssuer("foo.is/invalid"),153 ).154 PostClientRegister(cfg.OpenIDConfig.RegistrationEndpointAsString()).155 AssertStatusCodeBadRequest().156 Build(),157 ).158 TestCase(159 NewTestCaseBuilder("Register software client fails on invalid issuer too short").160 WithHttpClient(secureClient).161 GenerateSignedClaims(162 authoriserBuilder.163 WithIssuer(""),164 ).165 PostClientRegister(cfg.OpenIDConfig.RegistrationEndpointAsString()).166 AssertStatusCodeBadRequest().167 Build(),168 ).169 TestCase(170 NewTestCaseBuilder("Register software client fails on invalid issuer too long").171 WithHttpClient(secureClient).172 GenerateSignedClaims(173 authoriserBuilder.174 WithIssuer("123456789012345678901234567890"),175 ).176 PostClientRegister(cfg.OpenIDConfig.RegistrationEndpointAsString()).177 AssertStatusCodeBadRequest().178 Build(),179 ).180 TestCase(181 NewTestCaseBuilder("Register software client will fail with token endpoint auth method RS256").182 WithHttpClient(secureClient).183 GenerateSignedClaims(authoriserBuilder.WithTokenEndpointAuthMethod(jwt.SigningMethodRS256)).184 PostClientRegister(cfg.OpenIDConfig.RegistrationEndpointAsString()).185 AssertStatusCodeBadRequest().186 Build(),187 ).Build()188}189func DCR32RetrieveSoftwareClient(190 cfg DCR32Config,191 secureClient *http.Client,192 authoriserBuilder auth.AuthoriserBuilder,193 validator schema.Validator,194) Scenario {195 return NewBuilder(196 "DCR-005",197 "Dynamically retrieve a new software client",198 specLinkRetrieveSoftware,199 ).200 TestCase(DCR32CreateSoftwareClientTestCases(cfg, secureClient, authoriserBuilder)...).201 TestCase(DCR32RetrieveSoftwareClientTestCase(cfg, secureClient, validator)).202 TestCase(DCR32DeleteSoftwareClientTestCase(cfg, secureClient)).203 Build()204}205func DCR32RetrieveSoftwareClientTestCase(206 cfg DCR32Config,207 secureClient *http.Client,208 validator schema.Validator,209) TestCase {210 name := "Retrieve software client"211 if !cfg.GetImplemented {212 return NewTestCase(213 fmt.Sprintf("(SKIP Get endpoint not implemented) %s", name),214 []step.Step{},215 )216 }217 return NewTestCaseBuilder("Retrieve software client").218 WithHttpClient(secureClient).219 ClientRetrieve(cfg.OpenIDConfig.RegistrationEndpointAsString()).220 AssertStatusCodeOk().221 AssertValidSchemaResponse(validator).222 ParseClientRetrieveResponse(cfg.OpenIDConfig.TokenEndpoint).223 Build()224}225func DCR32RetrieveWithInvalidCredentials(226 cfg DCR32Config,227 secureClient *http.Client,228 authoriserBuilder auth.AuthoriserBuilder,229) Scenario {230 return NewBuilder(231 "DCR-007",232 "I should not be able to retrieve a software client with invalid credentials",233 specLinkRetrieveSoftware,234 ).235 TestCase(236 NewTestCaseBuilder("Register software client").237 WithHttpClient(secureClient).238 GenerateSignedClaims(authoriserBuilder).239 PostClientRegister(cfg.OpenIDConfig.RegistrationEndpointAsString()).240 AssertStatusCodeCreated().241 ParseClientRegisterResponse(authoriserBuilder).242 Build(),243 ).244 TestCase(245 NewTestCaseBuilder("Retrieve software client with invalid credentials grant").246 WithHttpClient(secureClient).247 SetInvalidGrantToken().248 ClientRetrieve(cfg.OpenIDConfig.RegistrationEndpointAsString()).249 AssertStatusCodeUnauthorized().250 Build(),251 ).252 TestCase(253 NewTestCaseBuilder("Retrieve client credentials grant").254 WithHttpClient(secureClient).255 GetClientCredentialsGrant(cfg.OpenIDConfig.TokenEndpoint).256 Build(),257 ).258 TestCase(DCR32DeleteSoftwareClientTestCase(cfg, secureClient)).259 Build()260}261func DCR32UpdateSoftwareClient(262 cfg DCR32Config,263 secureClient *http.Client,264 authoriserBuilder auth.AuthoriserBuilder,265) Scenario {266 id := "DCR-008"267 const name = "I should be able update a registered software"268 if !cfg.PutImplemented {269 return NewBuilder(270 id,271 fmt.Sprintf("(SKIP PUT endpoint not implemented) %s", name),272 specLinkRetrieveSoftware,273 ).Build()274 }275 return NewBuilder(276 id,277 name,278 specLinkUpdateSoftware,279 ).280 TestCase(DCR32CreateSoftwareClientTestCases(cfg, secureClient, authoriserBuilder)...).281 TestCase(282 NewTestCaseBuilder("Update an existing software client").283 WithHttpClient(secureClient).284 GenerateSignedClaims(authoriserBuilder).285 ClientUpdate(cfg.OpenIDConfig.RegistrationEndpointAsString()).286 AssertStatusCodeOk().287 Build(),288 ).289 TestCase(DCR32DeleteSoftwareClientTestCase(cfg, secureClient)).290 Build()291}292func DCR32UpdateSoftwareClientWithWrongId(293 cfg DCR32Config,294 secureClient *http.Client,295 authoriserBuilder auth.AuthoriserBuilder,296) Scenario {297 id := "DCR-009"298 const name = "When I try to update a non existing software client I should be unauthorized"299 if !cfg.PutImplemented {300 return NewBuilder(301 id,302 fmt.Sprintf("(SKIP PUT endpoint not implemented) %s", name),303 specLinkRetrieveSoftware,304 ).Build()305 }306 return NewBuilder(307 id,308 name,309 specLinkUpdateSoftware,310 ).311 TestCase(DCR32CreateSoftwareClientTestCases(cfg, secureClient, authoriserBuilder)...).312 TestCase(DCR32DeleteSoftwareClientTestCase(cfg, secureClient)).313 TestCase(314 NewTestCaseBuilder("Update a deleted software client").315 WithHttpClient(secureClient).316 GenerateSignedClaims(authoriserBuilder).317 ClientUpdate(cfg.OpenIDConfig.RegistrationEndpointAsString()).318 AssertStatusCodeUnauthorized().319 Build(),320 ).Build()321}322func DCR32RetrieveSoftwareClientWrongId(323 cfg DCR32Config,324 secureClient *http.Client,325 authoriserBuilder auth.AuthoriserBuilder,326) Scenario {327 id := "DCR-010"328 const name = "When I try to retrieve a non existing software client I should be unauthorized"329 return NewBuilder(330 id,331 name,332 specLinkUpdateSoftware,333 ).334 TestCase(DCR32CreateSoftwareClientTestCases(cfg, secureClient, authoriserBuilder)...).335 TestCase(DCR32DeleteSoftwareClientTestCase(cfg, secureClient)).336 TestCase(337 NewTestCaseBuilder("Retrieve a deleted software client").338 WithHttpClient(secureClient).339 ClientRetrieve(cfg.OpenIDConfig.RegistrationEndpointAsString()).340 AssertStatusCodeUnauthorized().341 Build(),342 ).Build()343}344func DCR32RegisterSoftwareWrongResponseType(345 cfg DCR32Config,346 secureClient *http.Client,347 authoriserBuilder auth.AuthoriserBuilder,348) Scenario {349 id := "DCR-011"350 const name = "When I try to register a software with invalid response_types it should be fail"351 return NewBuilder(352 id,353 name,354 specLinkRegisterSoftware,355 ).356 TestCase(357 NewTestCaseBuilder("Register software client").358 WithHttpClient(secureClient).359 GenerateSignedClaims(360 authoriserBuilder.WithResponseTypes([]string{"id_token", "token"}),361 ).362 PostClientRegister(cfg.OpenIDConfig.RegistrationEndpointAsString()).363 AssertStatusCodeBadRequest().364 ParseClientRegisterResponse(authoriserBuilder).365 Build(),366 ).367 Build()368}...

Full Screen

Full Screen

client_options.go

Source:client_options.go Github

copy

Full Screen

...36}37type withHTTPClient struct {38 x *http.Client39}40// WithHTTPClient 使用给定的 http.Client 作为 HTTP 客户端41func WithHTTPClient(client *http.Client) CtorOption {42 return &withHTTPClient{x: client}43}44var _ CtorOption = (*withHTTPClient)(nil)45func (x *withHTTPClient) applyTo(y *options) {46 y.HTTP = x.x47 y.restyCli = resty.NewWithClient(x.x)48}...

Full Screen

Full Screen

WithHTTPClient

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cfg := elasticsearch.Config{4 Addresses: []string{5 },6 Transport: &http.Transport{7 },8 }9 es, err := elasticsearch.NewClient(cfg)10 if err != nil {11 fmt.Println("Error creating the client: ", err)12 }13 fmt.Println(es.Info())14}15{200 OK 200 map[content-type:[application/json; charset=UTF-8] content-encoding:[gzip] content-length:[1081]] 0xc0000a2f80 1081 [] false false map[] map[] 0xc0000a2f80 <nil>}16es, err := elasticsearch.NewDefaultClient()17es, err := elasticsearch.NewDefaultClient()18es, err := elasticsearch.NewDefaultClient()19es, err := elasticsearch.NewDefaultClient()

Full Screen

Full Screen

WithHTTPClient

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cfg := elasticsearch.Config{4 Addresses: []string{5 },6 }7 httpClient := &http.Client{8 }9 es, err := elasticsearch.NewClient(cfg)10 if err != nil {11 log.Fatalf("Error creating the client: %s", err)12 }13 res, err := es.Cluster.Health(14 es.Cluster.Health.WithPretty(),15 if err != nil {16 log.Fatalf("Error getting response: %s", err)17 }18 defer res.Body.Close()19 if res.IsError() {20 log.Fatalf("Error: %s", res.String())21 }22 var r map[string]interface{}23 if err := esapi.DecodeBody(res, &r); err != nil {24 log.Fatalf("Error parsing the response body: %s", err)25 } else {26 log.Printf("[%s] %s; version=%d", res.Status(), r["cluster_name"], int(r["version"].(map[string]interface{})["number"].(float64)))27 }28}29[200 OK] elasticsearch; version=730import (

Full Screen

Full Screen

WithHTTPClient

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 es, err := elasticsearch.NewClient(elasticsearch.Config{4 Addresses: []string{5 },6 Transport: &http.Transport{7 },8 })9 if err != nil {10 panic(err)11 }12 info, err := es.Info()13 if err != nil {14 panic(err)15 }16 fmt.Printf("Client: %s17 fmt.Printf("Server: %s18 res, err := es.Cluster.Health()19 if err != nil {20 panic(err)21 }22 fmt.Printf("Cluster health: %s23}24Cluster health: {25}

Full Screen

Full Screen

WithHTTPClient

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := &http.Client{}4 fmt.Println(client.Timeout)5}6import (7func main() {8 client := &http.Client{}9 fmt.Println(client.Timeout)10}11import (12func main() {13 client := &http.Client{}14 fmt.Println(client.Timeout)15}16import (17func main() {18 client := &http.Client{}19 fmt.Println(client.Timeout)20}21import (22func main() {23 client := &http.Client{}24 fmt.Println(client.Timeout)25}26import (27func main() {28 client := &http.Client{}29 fmt.Println(client.Timeout)30}31import (32func main() {33 client := &http.Client{}34 fmt.Println(client.Timeout)35}36import (37func main() {38 client := &http.Client{}39 fmt.Println(client.Timeout)40}41import (42func main() {43 client := &http.Client{}44 fmt.Println(client.Timeout)45}46import (

Full Screen

Full Screen

WithHTTPClient

Using AI Code Generation

copy

Full Screen

1func main() {2 client := client.NewClient()3 if err != nil {4 panic(err)5 }6 httpClient := &http.Client{}7 client.WithHTTPClient(httpClient)8 resp, err := client.Send(req)9 if err != nil {10 panic(err)11 }12 fmt.Println(resp)13}14func main() {15 client := client.NewClient()16 if err != nil {17 panic(err)18 }19 httpClient := &http.Client{}20 client.SetHTTPClient(httpClient)21 resp, err := client.Send(req)22 if err != nil {23 panic(err)24 }25 fmt.Println(resp)26}27func main() {28 client := client.NewClient()29 if err != nil {30 panic(err)31 }32 httpClient := &http.Client{}33 req.WithHTTPClient(httpClient)34 resp, err := client.Send(req)35 if err != nil {36 panic(err)37 }38 fmt.Println(resp)39}40func main() {41 client := client.NewClient()42 if err != nil {43 panic(err)44 }

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful