Skip to content
This repository has been 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

Open
migueleliasweb opened this issue Jul 10, 2018 · 0 comments
Labels
enhancement New feature or request

Comments

@migueleliasweb
Copy link

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!

@dentarg dentarg added the enhancement New feature or request label May 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants