Example #1
0
// TestPlainHTTPServer verifies that we can serve plain http and talk to it.
// This is controlled by -cert=""
func TestPlainHTTPServer(t *testing.T) {
	defer leaktest.AfterTest(t)()
	s, _, _ := serverutils.StartServer(t, base.TestServerArgs{
		// The default context uses embedded certs.
		Insecure: true,
	})
	defer s.Stopper().Stop()
	ts := s.(*TestServer)

	httpClient, err := s.GetHTTPClient()
	if err != nil {
		t.Fatal(err)
	}

	httpURL := "http://" + ts.Ctx.HTTPAddr + healthPath
	if resp, err := httpClient.Get(httpURL); err != nil {
		t.Fatalf("error requesting health at %s: %s", httpURL, err)
	} else {
		defer resp.Body.Close()
		var data serverpb.HealthResponse
		if err := jsonpb.Unmarshal(resp.Body, &data); err != nil {
			t.Error(err)
		}
	}

	httpsURL := "https://" + ts.Ctx.HTTPAddr + healthPath
	if _, err := httpClient.Get(httpsURL); err == nil {
		t.Fatalf("unexpected success fetching %s", httpsURL)
	}
}
Example #2
0
// TestPlainHTTPServer verifies that we can serve plain http and talk to it.
// This is controlled by -cert=""
func TestPlainHTTPServer(t *testing.T) {
	defer leaktest.AfterTest(t)()
	// Create a custom context. The default one uses embedded certs.
	ctx := MakeContext()
	ctx.Addr = "127.0.0.1:0"
	ctx.HTTPAddr = "127.0.0.1:0"
	ctx.Insecure = true
	s := TestServer{Ctx: &ctx}
	if err := s.Start(); err != nil {
		t.Fatalf("could not start plain http server: %v", err)
	}
	defer s.Stop()

	httpClient, err := ctx.GetHTTPClient()
	if err != nil {
		t.Fatal(err)
	}

	httpURL := "http://" + s.Ctx.HTTPAddr + healthPath
	if resp, err := httpClient.Get(httpURL); err != nil {
		t.Fatalf("error requesting health at %s: %s", httpURL, err)
	} else {
		defer resp.Body.Close()
		var data serverpb.HealthResponse
		if err := jsonpb.Unmarshal(resp.Body, &data); err != nil {
			t.Error(err)
		}
	}

	httpsURL := "https://" + s.Ctx.HTTPAddr + healthPath
	if _, err := httpClient.Get(httpsURL); err == nil {
		t.Fatalf("unexpected success fetching %s", httpsURL)
	}
}
Example #3
0
// NewDecoder implements gwruntime.Marshaler.
func (j *JSONPb) NewDecoder(r io.Reader) gwruntime.Decoder {
	return gwruntime.DecoderFunc(func(v interface{}) error {
		if pb, ok := v.(proto.Message); ok {
			return jsonpb.Unmarshal(r, pb)
		}
		return errors.Errorf("unexpected type %T does not implement %s", v, typeProtoMessage)
	})
}
Example #4
0
// GetJSON uses the supplied client to GET the URL specified by the parameters
// and unmarshals the result into response.
func GetJSON(httpClient http.Client, path string, response proto.Message) error {
	resp, err := httpClient.Get(path)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		b, _ := ioutil.ReadAll(resp.Body)
		return errors.Errorf("status: %s, body: %s", resp.Status, b)
	}
	return jsonpb.Unmarshal(resp.Body, response)
}
Example #5
0
// TestAcceptEncoding hits the server while explicitly disabling
// decompression on a custom client's Transport and setting it
// conditionally via the request's Accept-Encoding headers.
func TestAcceptEncoding(t *testing.T) {
	defer leaktest.AfterTest(t)()
	s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
	defer s.Stopper().Stop()
	client, err := s.GetHTTPClient()
	if err != nil {
		t.Fatal(err)
	}

	testData := []struct {
		acceptEncoding string
		newReader      func(io.Reader) io.Reader
	}{
		{"",
			func(b io.Reader) io.Reader {
				return b
			},
		},
		{httputil.GzipEncoding,
			func(b io.Reader) io.Reader {
				r, err := gzip.NewReader(b)
				if err != nil {
					t.Fatalf("could not create new gzip reader: %s", err)
				}
				return r
			},
		},
	}
	for _, d := range testData {
		req, err := http.NewRequest("GET", s.AdminURL()+statusPrefix+"metrics/local", nil)
		if err != nil {
			t.Fatalf("could not create request: %s", err)
		}
		if d.acceptEncoding != "" {
			req.Header.Set(httputil.AcceptEncodingHeader, d.acceptEncoding)
		}
		resp, err := client.Do(req)
		if err != nil {
			t.Fatalf("could not make request to %s: %s", req.URL, err)
		}
		defer resp.Body.Close()
		if ce := resp.Header.Get(httputil.ContentEncodingHeader); ce != d.acceptEncoding {
			t.Fatalf("unexpected content encoding: '%s' != '%s'", ce, d.acceptEncoding)
		}
		r := d.newReader(resp.Body)
		var data serverpb.JSONResponse
		if err := jsonpb.Unmarshal(r, &data); err != nil {
			t.Error(err)
		}
	}
}
Example #6
0
// PostJSON uses the supplied client to POST request to the URL specified by
// the parameters and unmarshals the result into response .
func PostJSON(httpClient http.Client, path string, request, response proto.Message) error {
	str, err := (&jsonpb.Marshaler{}).MarshalToString(request)
	if err != nil {
		return err
	}
	resp, err := httpClient.Post(path, JSONContentType, strings.NewReader(str))
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		b, err := ioutil.ReadAll(resp.Body)
		return errors.Errorf("status: %s, body: %s, error: %s", resp.Status, b, err)
	}
	return jsonpb.Unmarshal(resp.Body, response)
}
Example #7
0
func doJSONRequest(httpClient http.Client, req *http.Request, response proto.Message) error {
	if timeout := httpClient.Timeout; timeout > 0 {
		req.Header.Set("Grpc-Timeout", strconv.FormatInt(timeout.Nanoseconds(), 10)+"n")
	}
	req.Header.Set(AcceptHeader, JSONContentType)
	resp, err := httpClient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if contentType := resp.Header.Get(ContentTypeHeader); !(resp.StatusCode == http.StatusOK && contentType == JSONContentType) {
		b, err := ioutil.ReadAll(resp.Body)
		return errors.Errorf("status: %s, content-type: %s, body: %s, error: %v", resp.Status, contentType, b, err)
	}
	return jsonpb.Unmarshal(resp.Body, response)
}
Example #8
0
// TestHealth verifies that health endpoint returns an empty JSON response.
func TestHealth(t *testing.T) {
	defer leaktest.AfterTest(t)()
	s := StartTestServer(t)
	defer s.Stop()
	u := s.Ctx.AdminURL() + healthPath
	httpClient, err := s.Ctx.GetHTTPClient()
	if err != nil {
		t.Fatal(err)
	}
	resp, err := httpClient.Get(u)
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()
	var data serverpb.HealthResponse
	if err := jsonpb.Unmarshal(resp.Body, &data); err != nil {
		t.Error(err)
	}
}
Example #9
0
// Unmarshal implements gwruntime.Marshaler.
func (j *JSONPb) Unmarshal(data []byte, v interface{}) error {
	if pb, ok := v.(proto.Message); ok {
		return jsonpb.Unmarshal(bytes.NewReader(data), pb)
	}
	return errors.Errorf("unexpected type %T does not implement %s", v, typeProtoMessage)
}
Example #10
0
// getRequestProto unmarshals the result of a get request to the test server
// with the given path.
func getRequestProto(t *testing.T, ts serverutils.TestServerInterface, path string, v proto.Message) error {
	respBody := getRequestReader(t, ts, path)
	defer respBody.Close()
	return jsonpb.Unmarshal(respBody, v)
}