...

Source file src/github.com/okta/okta-sdk-golang/v2/tests/testCommon.go

Documentation: github.com/okta/okta-sdk-golang/v2/tests

     1  /*
     2   * Copyright 2018 - Present Okta, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package tests
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net/http"
    23  	"testing"
    24  
    25  	"github.com/jarcoal/httpmock"
    26  	"github.com/okta/okta-sdk-golang/v2/okta"
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func NewClient(ctx context.Context, conf ...okta.ConfigSetter) (context.Context, *okta.Client, error) {
    32  	return okta.NewClient(ctx, conf...)
    33  }
    34  
    35  func MockResponse(responses ...*http.Response) httpmock.Responder {
    36  	return func(req *http.Request) (*http.Response, error) {
    37  		httpmock.GetTotalCallCount()
    38  		info := httpmock.GetCallCountInfo()
    39  		count := info[req.Method+" "+req.URL.Path]
    40  
    41  		if len(responses) >= count {
    42  			return responses[count-1], nil
    43  		}
    44  
    45  		return nil, fmt.Errorf("no response found for call %v to %s", count, req.URL.Path)
    46  	}
    47  }
    48  
    49  func AssertResponse(t *testing.T, response *okta.Response, requestMethod string, requestPath string) {
    50  	require.IsType(t, &okta.Response{}, response, "did not return `*okta.Response` type as second variable")
    51  	assert.Equal(t, requestMethod, response.Response.Request.Method, "did not make a requestMethod request")
    52  	assert.Equal(t, requestPath, response.Response.Request.URL.Path, "path for request was incorrect")
    53  }
    54  

View as plain text