<commit_before>package gateway

import (
	"bytes"
	"net/http"
	"net/http/httptest"
	"time"

	"github.com/backstage/maestro/account"
	. "gopkg.in/check.v1"
)

func (s *S) TestGatewayNotFound(c *C) {
	gateway := New(s.Settings, nil)
	w := httptest.NewRecorder()
	w.Body = new(bytes.Buffer)
	r, _ := http.NewRequest("GET", "invalid.backstage.dev", nil)
	gateway.ServeHTTP(w, r)

	c.Assert(w.Code, Equals, http.StatusNotFound)
	c.Assert(w.Header().Get("Content-Type"), Equals, "application/json")
	c.Assert(w.Body.String(), Equals, "{\"error\":\"not_found\",\"error_description\":\"The requested resource could not be found but may be available again in the future.\"}\n")
}

func (s *S) TestGatewayExistingService(c *C) {
	target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("OK"))
		c.Assert(len(r.Header.Get("X-Request-Id")), Not(Equals), 0)
	}))
	defer target.Close()

	services := []*account.Service{&account.Service{Endpoint: "http://" + target.Listener.Addr().String(), Subdomain: "test"}}
	gateway := New(s.Settings, nil)
	gateway.LoadServices(services)
	w := httptest.NewRecorder()
	w.Body = new(bytes.Buffer)
	r, _ := http.NewRequest("GET", "http://test.backstage.dev", nil)
	gateway.ServeHTTP(w, r)

	c.Assert(w.Body.String(), Equals, "OK")
	c.Assert(w.Code, Equals, http.StatusOK)
}

func (s *S) TestGatewayTimeout(c *C) {
	target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(2 * time.Second)
	}))
	defer target.Close()

	services := []*account.Service{&account.Service{Endpoint: "http://" + target.Listener.Addr().String(), Subdomain: "test", Timeout: 1}}
	gateway := New(s.Settings, nil)
	gateway.LoadServices(services)

	w := httptest.NewRecorder()
	w.Body = new(bytes.Buffer)
	r, _ := http.NewRequest("GET", "http://test.backstage.dev", nil)
	gateway.ServeHTTP(w, r)

	c.Assert(w.Code, Equals, http.StatusGatewayTimeout)
	c.Assert(w.Header().Get("Content-Type"), Equals, "application/json")
	c.Assert(w.Body.String(), Equals, "{\"error\":\"gateway_timeout\",\"error_description\":\"The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server.\"}")
}

func (s *S) TestGatewayCopyResponseHeaders(c *C) {
	target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusCreated)
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	}))
	defer target.Close()

	services := []*account.Service{&account.Service{Endpoint: "http://" + target.Listener.Addr().String(), Subdomain: "test"}}
	gateway := New(s.Settings, nil)
	gateway.LoadServices(services)

	w := httptest.NewRecorder()
	w.Body = new(bytes.Buffer)
	r, _ := http.NewRequest("GET", "http://test.backstage.dev", nil)
	gateway.ServeHTTP(w, r)

	c.Assert(w.Code, Equals, http.StatusCreated)
	c.Assert(w.Header().Get("Content-Type"), Equals, "text/plain; charset=utf-8")
}

func (s *S) TestGatewayInternalError(c *C) {
	services := []*account.Service{&account.Service{Endpoint: "http://invalidurl", Subdomain: "test"}}
	gateway := New(s.Settings, nil)
	gateway.LoadServices(services)

	w := httptest.NewRecorder()
	w.Body = new(bytes.Buffer)
	r, _ := http.NewRequest("GET", "http://test.backstage.dev", nil)
	gateway.ServeHTTP(w, r)

	c.Assert(w.Code, Equals, http.StatusInternalServerError)
	c.Assert(w.Header().Get("Content-Type"), Equals, "application/json")
	c.Assert(w.Body.String(), Equals, "{\"error\":\"internal_server_error\",\"error_description\":\"dial tcp: lookup invalidurl: no such host\"}")
}
<commit_msg>Add and Remove service on Maestro.<commit_after>package gateway

import (
	"bytes"
	"net/http"
	"net/http/httptest"
	"time"

	"github.com/backstage/maestro/account"
	. "gopkg.in/check.v1"
)

func (s *S) TestGatewayNotFound(c *C) {
	gateway := New(s.Settings, nil)
	w := httptest.NewRecorder()
	w.Body = new(bytes.Buffer)
	r, _ := http.NewRequest("GET", "invalid.backstage.dev", nil)
	gateway.ServeHTTP(w, r)

	c.Assert(w.Code, Equals, http.StatusNotFound)
	c.Assert(w.Header().Get("Content-Type"), Equals, "application/json")
	c.Assert(w.Body.String(), Equals, "{\"error\":\"not_found\",\"error_description\":\"The requested resource could not be found but may be available again in the future.\"}\n")
}

func (s *S) TestGatewayExistingService(c *C) {
	target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("OK"))
		c.Assert(len(r.Header.Get("X-Request-Id")), Not(Equals), 0)
	}))
	defer target.Close()

	services := []*account.Service{&account.Service{Endpoint: "http://" + target.Listener.Addr().String(), Subdomain: "test"}}
	gateway := New(s.Settings, nil)
	gateway.LoadServices(services)
	w := httptest.NewRecorder()
	w.Body = new(bytes.Buffer)
	r, _ := http.NewRequest("GET", "http://test.backstage.dev", nil)
	gateway.ServeHTTP(w, r)

	c.Assert(w.Body.String(), Equals, "OK")
	c.Assert(w.Code, Equals, http.StatusOK)
}

func (s *S) TestGatewayTimeout(c *C) {
	target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(2 * time.Second)
	}))
	defer target.Close()

	services := []*account.Service{&account.Service{Endpoint: "http://" + target.Listener.Addr().String(), Subdomain: "test", Timeout: 1}}
	gateway := New(s.Settings, nil)
	gateway.LoadServices(services)

	w := httptest.NewRecorder()
	w.Body = new(bytes.Buffer)
	r, _ := http.NewRequest("GET", "http://test.backstage.dev", nil)
	gateway.ServeHTTP(w, r)

	c.Assert(w.Code, Equals, http.StatusGatewayTimeout)
	c.Assert(w.Header().Get("Content-Type"), Equals, "application/json")
	c.Assert(w.Body.String(), Equals, `{"error":"gateway_timeout","error_description":"The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server."}`)
}

func (s *S) TestGatewayCopyResponseHeaders(c *C) {
	target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusCreated)
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	}))
	defer target.Close()

	services := []*account.Service{&account.Service{Endpoint: "http://" + target.Listener.Addr().String(), Subdomain: "test"}}
	gateway := New(s.Settings, nil)
	gateway.LoadServices(services)

	w := httptest.NewRecorder()
	w.Body = new(bytes.Buffer)
	r, _ := http.NewRequest("GET", "http://test.backstage.dev", nil)
	gateway.ServeHTTP(w, r)

	c.Assert(w.Code, Equals, http.StatusCreated)
	c.Assert(w.Header().Get("Content-Type"), Equals, "text/plain; charset=utf-8")
}

func (s *S) TestGatewayInternalError(c *C) {
	services := []*account.Service{&account.Service{Endpoint: "http://invalidurl", Subdomain: "test"}}
	gateway := New(s.Settings, nil)
	gateway.LoadServices(services)

	w := httptest.NewRecorder()
	w.Body = new(bytes.Buffer)
	r, _ := http.NewRequest("GET", "http://test.backstage.dev", nil)
	gateway.ServeHTTP(w, r)

	c.Assert(w.Code, Equals, http.StatusInternalServerError)
	c.Assert(w.Header().Get("Content-Type"), Equals, "application/json")
	c.Assert(w.Body.String(), Equals, `{"error":"internal_server_error","error_description":"dial tcp: lookup invalidurl: no such host"}`)
}

func (s *S) TestAddService(c *C) {
	target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("OK"))
		c.Assert(len(r.Header.Get("X-Request-Id")), Not(Equals), 0)
	}))
	defer target.Close()

	service := &account.Service{Endpoint: "http://" + target.Listener.Addr().String(), Subdomain: "test"}
	gateway := New(s.Settings, nil)
	gateway.AddService(service)
	w := httptest.NewRecorder()
	w.Body = new(bytes.Buffer)
	r, _ := http.NewRequest("GET", "http://test.backstage.dev", nil)
	gateway.ServeHTTP(w, r)

	c.Assert(w.Body.String(), Equals, "OK")
	c.Assert(w.Code, Equals, http.StatusOK)
}

func (s *S) TestRemoveService(c *C) {
	target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("OK"))
		c.Assert(len(r.Header.Get("X-Request-Id")), Not(Equals), 0)
	}))
	defer target.Close()

	service := &account.Service{Endpoint: "http://" + target.Listener.Addr().String(), Subdomain: "test"}
	gateway := New(s.Settings, nil)
	gateway.AddService(service)
	w := httptest.NewRecorder()
	w.Body = new(bytes.Buffer)
	r, _ := http.NewRequest("GET", "http://test.backstage.dev", nil)
	gateway.ServeHTTP(w, r)
	c.Assert(w.Body.String(), Equals, "OK")
	c.Assert(w.Code, Equals, http.StatusOK)

	gateway.RemoveService(service)
	r, _ = http.NewRequest("GET", "http://test.backstage.dev", nil)
	w = httptest.NewRecorder()
	gateway.ServeHTTP(w, r)
	c.Assert(w.Body.String(), Equals, "{\"error\":\"not_found\",\"error_description\":\"The requested resource could not be found but may be available again in the future.\"}\n")
	c.Assert(w.Code, Equals, http.StatusNotFound)
}

{'fraction_non_alphanumeric': 0.19499831023994593, 'fraction_numerical': 0.0015771093838008337, 'mean_word_length': 5.274204946996466, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 3, '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': '6402027', 'n_tokens_mistral': 3299, 'n_tokens_neox': 3246, 'n_words': 592}