-
Notifications
You must be signed in to change notification settings - Fork 479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix the Route Prefix bug and add ExternalURL Support (#190) #239
Merged
beorn7
merged 7 commits into
prometheus:master
from
apghero:apghero/bugfix/190/route-prefix-bug
Apr 2, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e61ed49
Addresses route-prefix bug described in #190
apghero ebf02f6
Add some tests for modified Static.
apghero e5fd860
Add License header to new test file.
apghero 91234c2
Provide support for --web.external-url
apghero 5c60598
Update copyright dates.
apghero 0f0cc1f
Update documentation based on feedback.
apghero e747fea
Precompute BaseURL instead of passing `join` into template.
apghero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The inline comments in this function are essentially just describing the (very simple) code and thus make it actually less readable. I guess the point you are trying to make is that this code implements the spec given by the flags. How about turning them into a doc comment before the function: