<commit_before>// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package api_test

import (
	"net/http"

	gc "gopkg.in/check.v1"

	"github.com/juju/juju/api"
	apihttp "github.com/juju/juju/api/http"
	apihttptesting "github.com/juju/juju/api/http/testing"
	jujutesting "github.com/juju/juju/juju/testing"
	"github.com/juju/juju/provider/dummy"
)

type httpSuite struct {
	apihttptesting.BaseSuite
	jujutesting.JujuConnSuite
}

var _ = gc.Suite(&httpSuite{})

func (s *httpSuite) SetUpTest(c *gc.C) {
	s.BaseSuite.SetUpTest(c)
	s.JujuConnSuite.SetUpTest(c)

	// This determines the client used in SendHTTPRequest().
	s.PatchValue(api.NewHTTPClient,
		func(*api.State) apihttp.HTTPClient {
			return s.Fake
		},
	)
}

func (s *httpSuite) checkRequest(c *gc.C, req *http.Request, method, pth string) {
	username := dummy.AdminUserTag().String()
	password := <PASSWORD>
	hostname := "localhost"
	s.CheckRequest(c, req, method, username, password, hostname, pth)
}

func (s *httpSuite) TestNewHTTPRequestSuccess(c *gc.C) {
	req, err := s.APIState.NewHTTPRequest("GET", "somefacade")
	c.Assert(err, gc.IsNil)

	s.checkRequest(c, req, "GET", "somefacade")
}

func (s *httpSuite) TestNewHTTPClientCorrectTransport(c *gc.C) {
	httpClient := s.APIState.NewHTTPClient()

	c.Assert(httpClient.Transport, gc.NotNil)
	c.Assert(httpClient.Transport, gc.FitsTypeOf, (*http.Transport)(nil))
	config := httpClient.Transport.(*http.Transport).TLSClientConfig

	c.Check(config.RootCAs, gc.NotNil)
}

func (s *httpSuite) TestNewHTTPClientValidatesCert(c *gc.C) {
	req, err := s.APIState.NewHTTPRequest("GET", "somefacade")
	httpClient := s.APIState.NewHTTPClient()
	resp, err := httpClient.Do(req)
	c.Assert(err, gc.IsNil)

	c.Check(resp.StatusCode, gc.Equals, http.StatusNotFound)
}

func (s *httpSuite) TestSendHTTPRequestSuccess(c *gc.C) {
	req, resp, err := s.APIState.SendHTTPRequest("GET", "somefacade", nil)
	c.Assert(err, gc.IsNil)

	s.Fake.CheckCalled(c, req, resp)
}
<commit_msg>pth -> path.<commit_after>// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package api_test

import (
	"net/http"

	gc "gopkg.in/check.v1"

	"github.com/juju/juju/api"
	apihttp "github.com/juju/juju/api/http"
	apihttptesting "github.com/juju/juju/api/http/testing"
	jujutesting "github.com/juju/juju/juju/testing"
	"github.com/juju/juju/provider/dummy"
)

type httpSuite struct {
	apihttptesting.BaseSuite
	jujutesting.JujuConnSuite
}

var _ = gc.Suite(&httpSuite{})

func (s *httpSuite) SetUpTest(c *gc.C) {
	s.BaseSuite.SetUpTest(c)
	s.JujuConnSuite.SetUpTest(c)

	// This determines the client used in SendHTTPRequest().
	s.PatchValue(api.NewHTTPClient,
		func(*api.State) apihttp.HTTPClient {
			return s.Fake
		},
	)
}

func (s *httpSuite) checkRequest(c *gc.C, req *http.Request, method, path string) {
	username := dummy.AdminUserTag().String()
	password := <PASSWORD>
	hostname := "localhost"
	s.CheckRequest(c, req, method, username, password, hostname, path)
}

func (s *httpSuite) TestNewHTTPRequestSuccess(c *gc.C) {
	req, err := s.APIState.NewHTTPRequest("GET", "somefacade")
	c.Assert(err, gc.IsNil)

	s.checkRequest(c, req, "GET", "somefacade")
}

func (s *httpSuite) TestNewHTTPClientCorrectTransport(c *gc.C) {
	httpClient := s.APIState.NewHTTPClient()

	c.Assert(httpClient.Transport, gc.NotNil)
	c.Assert(httpClient.Transport, gc.FitsTypeOf, (*http.Transport)(nil))
	config := httpClient.Transport.(*http.Transport).TLSClientConfig

	c.Check(config.RootCAs, gc.NotNil)
}

func (s *httpSuite) TestNewHTTPClientValidatesCert(c *gc.C) {
	req, err := s.APIState.NewHTTPRequest("GET", "somefacade")
	httpClient := s.APIState.NewHTTPClient()
	resp, err := httpClient.Do(req)
	c.Assert(err, gc.IsNil)

	c.Check(resp.StatusCode, gc.Equals, http.StatusNotFound)
}

func (s *httpSuite) TestSendHTTPRequestSuccess(c *gc.C) {
	req, resp, err := s.APIState.SendHTTPRequest("GET", "somefacade", nil)
	c.Assert(err, gc.IsNil)

	s.Fake.CheckCalled(c, req, resp)
}

{'fraction_non_alphanumeric': 0.1528709917971663, 'fraction_numerical': 0.002982848620432513, 'mean_word_length': 4.997019374068555, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4844867', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1557, 'n_words': 287}