-
Notifications
You must be signed in to change notification settings - Fork 46
Add support for specifying which browsers in /api/runs #280
Changes from all commits
0cf1a0e
45a4d97
700388c
81e1c44
7270de2
dc70c7f
57947bd
8c8aebe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2017 Google Inc. | ||
// | ||
// 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 wptdashboard | ||
|
||
package wptdashboard | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"regexp" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
// SHARegex is a regex for SHA[0:10] slice of a git hash. | ||
var SHARegex = regexp.MustCompile("[0-9a-fA-F]{10}") | ||
|
||
// ParseSHAParam parses and validates the 'sha' param for the request. | ||
// It returns "latest" by default (and in error cases). | ||
func ParseSHAParam(r *http.Request) (runSHA string, err error) { | ||
// Get the SHA for the run being loaded (the first part of the path.) | ||
runSHA = "latest" | ||
params, err := url.ParseQuery(r.URL.RawQuery) | ||
if err != nil { | ||
return runSHA, err | ||
} | ||
|
||
runParam := params.Get("sha") | ||
if SHARegex.MatchString(runParam) { | ||
runSHA = runParam | ||
} | ||
return runSHA, err | ||
} | ||
|
||
// ParseBrowserParam parses and validates the 'browser' param for the request. | ||
// It returns "" by default (and in error cases). | ||
func ParseBrowserParam(r *http.Request) (browser string, err error) { | ||
browser = r.URL.Query().Get("browser") | ||
if "" == browser { | ||
return "", nil | ||
} | ||
if IsBrowserName(browser) { | ||
return browser, nil | ||
} | ||
return "", fmt.Errorf("invalid browser param %s", browser) | ||
} | ||
|
||
// ParseBrowsersParam returns a sorted list of browsers to include. | ||
// It parses the 'browsers' parameter, split on commas, and also checks for the (repeatable) 'browser' params, | ||
// before falling back to the default set of browsers. | ||
func ParseBrowsersParam(r *http.Request) (browsers []string, err error) { | ||
browsers = r.URL.Query()["browser"] | ||
if browsersParam := r.URL.Query().Get("browsers"); browsersParam != "" { | ||
browsers = append(browsers, strings.Split(browsersParam, ",")...) | ||
} | ||
// If no params found, return the default. | ||
var browserNames []string | ||
if browserNames, err = GetBrowserNames(); err != nil { | ||
return nil, err | ||
} | ||
if len(browsers) == 0 { | ||
return browserNames, nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto (logging) |
||
} | ||
// Otherwise filter to valid browser names. | ||
for i := 0; i < len(browsers); { | ||
if !IsBrowserName(browsers[i]) { | ||
// 'Remove' browser by switching to end and cropping. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you say so 😃 @jeffcarp can you please golang-review this bit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Log this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed #283 to include this. |
||
browsers[len(browsers)-1], browsers[i] = browsers[i], browsers[len(browsers)-1] | ||
browsers = browsers[:len(browsers)-1] | ||
continue | ||
} | ||
i++ | ||
} | ||
sort.Strings(browsers) | ||
return browsers, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright 2017 Google Inc. | ||
// | ||
// 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 wptdashboard | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestParseSHAParam(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/", nil) | ||
runSHA, err := ParseSHAParam(r) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "latest", runSHA) | ||
} | ||
|
||
func TestParseSHAParam_2(t *testing.T) { | ||
sha := "0123456789" | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?sha="+sha, nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These files are gofmt'd, that's the way they like it 😕 |
||
runSHA, err := ParseSHAParam(r) | ||
assert.Nil(t, err) | ||
assert.Equal(t, sha, runSHA) | ||
} | ||
|
||
func TestParseSHAParam_BadRequest(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?sha=%zz", nil) | ||
runSHA, err := ParseSHAParam(r) | ||
assert.NotNil(t, err) | ||
assert.Equal(t, "latest", runSHA) | ||
} | ||
|
||
func TestParseSHAParam_NonSHA(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?sha=123", nil) | ||
runSHA, err := ParseSHAParam(r) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "latest", runSHA) | ||
} | ||
|
||
func TestParseSHAParam_NonSHA_2(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?sha=zapper0123", nil) | ||
runSHA, err := ParseSHAParam(r) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "latest", runSHA) | ||
} | ||
|
||
func TestParseBrowserParam(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/", nil) | ||
browser, err := ParseBrowserParam(r) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "", browser) | ||
} | ||
|
||
func TestParseBrowserParam_Chrome(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?browser=chrome", nil) | ||
browser, err := ParseBrowserParam(r) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "chrome", browser) | ||
} | ||
|
||
func TestParseBrowserParam_Invalid(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?browser=invalid", nil) | ||
browser, err := ParseBrowserParam(r) | ||
assert.NotNil(t, err) | ||
assert.Equal(t, "", browser) | ||
} | ||
|
||
func TestParseBrowsersParam(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/", nil) | ||
browsers, err := ParseBrowsersParam(r) | ||
assert.Nil(t, err) | ||
defaultBrowsers, err := GetBrowserNames() | ||
assert.Equal(t, defaultBrowsers, browsers) | ||
} | ||
|
||
func TestParseBrowsersParam_ChromeSafari(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?browsers=chrome,safari", nil) | ||
browsers, err := ParseBrowsersParam(r) | ||
assert.Nil(t, err) | ||
assert.Equal(t, []string{"chrome", "safari"}, browsers) | ||
} | ||
|
||
func TestParseBrowsersParam_ChromeInvalid(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?browsers=chrome,invalid", nil) | ||
browsers, err := ParseBrowsersParam(r) | ||
assert.Nil(t, err) | ||
assert.Equal(t, []string{"chrome"}, browsers) | ||
} | ||
|
||
func TestParseBrowsersParam_AllInvalid(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?browsers=notabrowser,invalid", nil) | ||
browsers, err := ParseBrowsersParam(r) | ||
assert.Nil(t, err) | ||
assert.Empty(t, browsers) | ||
} | ||
|
||
func TestParseBrowsersParam_EmptyCommas(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?browsers=,notabrowser,,,,invalid,,", nil) | ||
browsers, err := ParseBrowsersParam(r) | ||
assert.Nil(t, err) | ||
assert.Empty(t, browsers) | ||
} | ||
|
||
func TestParseBrowsersParam_SafariChrome(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?browsers=safari,chrome", nil) | ||
browsers, err := ParseBrowsersParam(r) | ||
assert.Nil(t, err) | ||
assert.Equal(t, []string{"chrome", "safari"}, browsers) | ||
} | ||
|
||
func TestParseBrowsersParam_MultiBrowserParam_SafariChrome(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?browser=safari&browser=chrome", nil) | ||
browsers, err := ParseBrowsersParam(r) | ||
assert.Nil(t, err) | ||
assert.Equal(t, []string{"chrome", "safari"}, browsers) | ||
} | ||
|
||
func TestParseBrowsersParam_MultiBrowserParam_SafariInvalid(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?browser=safari&browser=invalid", nil) | ||
browsers, err := ParseBrowsersParam(r) | ||
assert.Nil(t, err) | ||
assert.Equal(t, []string{"safari"}, browsers) | ||
} | ||
|
||
func TestParseBrowsersParam_MultiBrowserParam_AllInvalid(t *testing.T) { | ||
r := httptest.NewRequest("GET", "http://wpt.fyi/?browser=invalid&browser=notabrowser", nil) | ||
browsers, err := ParseBrowsersParam(r) | ||
assert.Nil(t, err) | ||
assert.Empty(t, browsers) | ||
} |
This file was deleted.
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.
@jeffcarp is this special case of multiple-statements-on-one-line-with-error-checking a standard golang idiom?
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.
Yep, they're called 'short statements' - https://tour.golang.org/flowcontrol/6