-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Addresses route-prefix bug described in #190 Pass in the prefix to the Static handler such that it can be removed from the Path looked up to serve files relative to the FileSystem's root. * Provide support for --web.external-url This works in conjunction with the `--web.route-prefix` flag to provide resources at the provided external URL, with the properly munged `route-prefix`. If an external URL is provided, but a `route-prefix` is not, this change takes the external URL's Path and uses that as the `route-prefix`. Includes a simple test checking for the presence of a known external-url in the body of the Status handler. Signed-off-by: Andrew G10z <[email protected]>
- Loading branch information
Showing
7 changed files
with
201 additions
and
38 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2019 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package handler | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
) | ||
|
||
type fakeFileSystem struct { | ||
files map[string]struct{} | ||
} | ||
|
||
// Open implements the http.FileSystem interface | ||
// | ||
// If a file is present, no error will be returned. | ||
// This implementation always returns a nil File. | ||
func (f *fakeFileSystem) Open(name string) (http.File, error) { | ||
log.Println("requesting" + name) | ||
|
||
if _, ok := f.files[name]; !ok { | ||
return nil, os.ErrNotExist | ||
} | ||
return os.Open("misc_test.go") | ||
// return nil, nil | ||
} | ||
|
||
func TestRoutePrefixForStatic(t *testing.T) { | ||
fs := &fakeFileSystem{map[string]struct{}{ | ||
"/index.js": struct{}{}, | ||
}} | ||
|
||
for _, test := range []struct { | ||
prefix string | ||
path string | ||
code int | ||
}{ | ||
{"/", "/index.js", 200}, | ||
{"/", "/missing.js", 404}, | ||
{"/route-prefix", "/index.js", 200}, | ||
{"/route-prefix", "/missing.js", 404}, | ||
} { | ||
test := test | ||
t.Run(fmt.Sprintf("%v", test), func(t *testing.T) { | ||
t.Parallel() | ||
req, err := http.NewRequest( | ||
http.MethodGet, "http://example.com"+test.prefix+test.path, nil, | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
w := httptest.NewRecorder() | ||
static := Static(fs, test.prefix) | ||
static.ServeHTTP(w, req) | ||
if test.code != w.Code { | ||
t.Errorf("Wanted %d, got %d.", test.code, w.Code) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2019 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package handler | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/pushgateway/asset" | ||
"github.com/prometheus/pushgateway/storage" | ||
) | ||
|
||
func TestExternalURLPresenceInPage(t *testing.T) { | ||
flags := map[string]string{ | ||
"web.listen-address": ":9091", | ||
"web.telemetry-path": "/metrics", | ||
"web.external-url": "http://web-external-url.com", | ||
} | ||
|
||
ms := storage.NewDiskMetricStore("", time.Minute, nil) | ||
status := Status(ms, asset.Assets, flags) | ||
defer ms.Shutdown() | ||
|
||
w := httptest.NewRecorder() | ||
status.ServeHTTP(w, &http.Request{}) | ||
|
||
if http.StatusOK != w.Code { | ||
t.Fatalf("Wanted status %d, got %d", http.StatusOK, w.Code) | ||
} | ||
|
||
var rawBody []byte | ||
w.Result().Body.Read(rawBody) | ||
body := string(rawBody) | ||
|
||
if index := strings.Index(body, flags["web.external-url"]); index > 0 { | ||
t.Errorf("Wanted index of %q > 0 , got %d", flags["web.external-url"], index) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters