Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Unable to inject different "sling" property to API due to unexported property #1

@migueleliasweb

Description

@migueleliasweb

Hey guys,

I'm was playing around with https://github.com/cloudamqp/terraform-provider and trying to write some unittests and stumbled across a possible issue.

Have a look how the api variable is casted from interface{} to *.api.API:

resource_image.go
func resourceCreate(d *schema.ResourceData, meta interface{}) error {
	api := meta.(*api.API)
	keys := []string{"name", "plan", "region", "nodes"}
        #[...]
}

This would be easy to test if I could inject the api client with a fake mux to actually test the requests but the underlying code looks like this:

api/api.go
type API struct {
	sling *sling.Sling
}

func New(baseUrl, apiKey string) *API {
	return &API{
		sling: sling.New().
			Client(http.DefaultClient).
			Base(baseUrl).
			SetBasicAuth("", apiKey),
	}
}

Written this way, I seem to be unable to use a different Client when defining a custom API struct since the sling property was not exported.

Is there another way to test without injecting the sling client? If so, how can I do it?

Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions