Skip to content
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

chore: push changes to main #101

Merged
merged 6 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ name: "CodeQL"

on:
push:
branches: [ main ]
branches: [ main, develop ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
branches: [ main, develop ]
schedule:
- cron: '33 8 * * 4'

Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ jobs:
uses: actions/checkout@v3
with:
fetch-depth: 0
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
-
name: Set up Go
uses: actions/setup-go@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sonar.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Sonar
on:
push:
branches: [ main ]
branches: [ main, develop ]

jobs:
sonar:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: Test
on:
pull_request:
branches: [ main ]
branches: [ main, develop ]
types: [opened, synchronize, reopened]
push:
branches: [ main ]
branches: [ main, develop ]

jobs:
test:
Expand Down
6 changes: 3 additions & 3 deletions check/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c *FTWCheck) SetNoLogContains(contains string) {

// ForcedIgnore check if this id need to be ignored from results
func (c *FTWCheck) ForcedIgnore(id string) bool {
for re, _ := range c.overrides.Ignore {
for re := range c.overrides.Ignore {
if re.MatchString(id) {
return true
}
Expand All @@ -70,7 +70,7 @@ func (c *FTWCheck) ForcedIgnore(id string) bool {

// ForcedPass check if this id need to be ignored from results
func (c *FTWCheck) ForcedPass(id string) bool {
for re, _ := range c.overrides.ForcePass {
for re := range c.overrides.ForcePass {
if re.MatchString(id) {
return true
}
Expand All @@ -80,7 +80,7 @@ func (c *FTWCheck) ForcedPass(id string) bool {

// ForcedFail check if this id need to be ignored from results
func (c *FTWCheck) ForcedFail(id string) bool {
for re, _ := range c.overrides.ForceFail {
for re := range c.overrides.ForceFail {
if re.MatchString(id) {
return true
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ var runCmd = &cobra.Command{
excludeRE = regexp.MustCompile(exclude)
}

currentRun := runner.Run(tests, runner.Config{
currentRun, err := runner.Run(tests, runner.Config{
Include: includeRE,
Exclude: excludeRE,
ShowTime: showTime,
Quiet: quiet,
ConnectTimeout: connectTimeout,
ReadTimeout: readTimeout,
})
if err != nil {
log.Fatal().Err(err)
}

os.Exit(currentRun.Stats.TotalFailed())
},
Expand Down
14 changes: 14 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ func NewConfigFromFile(cfgFile string) error {
cfgFile = ".ftw.yaml"
}

_, err = os.Stat(cfgFile)
if err != nil { // file does not exist, so we try the home folder

var home string

home, err = os.UserHomeDir()
if err != nil { // home folder could not be retrieved
return err
}

cfgFile = home + "/.ftw.yaml"

}

_, err = os.Stat(cfgFile)
if err != nil { // file exists, so we read it looking for config values
return err
Expand Down
6 changes: 3 additions & 3 deletions ftwhttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ func NewClientConfig() ClientConfig {
}

// NewClient initializes the http client, creating the cookiejar
func NewClient(config ClientConfig) *Client {
func NewClient(config ClientConfig) (*Client, error) {
// All users of cookiejar should import "golang.org/x/net/publicsuffix"
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
log.Fatal().Err(err)
return nil, err
}
c := &Client{
Jar: jar,
config: config,
}
return c
return c, nil
}

// NewConnection creates a new Connection based on a Destination
Expand Down
32 changes: 20 additions & 12 deletions ftwhttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

func TestNewClient(t *testing.T) {
c := NewClient(NewClientConfig())
c, err := NewClient(NewClientConfig())
assert.NoError(t, err)

assert.NotNil(t, c.Jar, "Error creating Client")
}
Expand All @@ -19,9 +20,10 @@ func TestConnectDestinationHTTPS(t *testing.T) {
Protocol: "https",
}

c := NewClient(NewClientConfig())
c, err := NewClient(NewClientConfig())
assert.NoError(t, err)

err := c.NewConnection(*d)
err = c.NewConnection(*d)
assert.NoError(t, err, "This should not error")
assert.Equal(t, "https", c.Transport.protocol, "Error connecting to example.com using https")
}
Expand All @@ -33,11 +35,12 @@ func TestDoRequest(t *testing.T) {
Protocol: "https",
}

c := NewClient(NewClientConfig())
c, err := NewClient(NewClientConfig())
assert.NoError(t, err)

req := generateBaseRequestForTesting()

err := c.NewConnection(*d)
err = c.NewConnection(*d)
assert.NoError(t, err, "This should not error")

_, err = c.Do(*req)
Expand All @@ -52,7 +55,8 @@ func TestGetTrackedTime(t *testing.T) {
Protocol: "https",
}

c := NewClient(NewClientConfig())
c, err := NewClient(NewClientConfig())
assert.NoError(t, err)

rl := &RequestLine{
Method: "POST",
Expand All @@ -65,7 +69,7 @@ func TestGetTrackedTime(t *testing.T) {
data := []byte(`test=me&one=two&one=twice`)
req := NewRequest(rl, h, data, true)

err := c.NewConnection(*d)
err = c.NewConnection(*d)
assert.NoError(t, err, "This should not error")

c.StartTrackingTime()
Expand All @@ -90,7 +94,8 @@ func TestClientMultipartFormDataRequest(t *testing.T) {
Protocol: "https",
}

c := NewClient(NewClientConfig())
c, err := NewClient(NewClientConfig())
assert.NoError(t, err)

rl := &RequestLine{
Method: "POST",
Expand All @@ -112,7 +117,7 @@ Some-file-test-here

req := NewRequest(rl, h, data, true)

err := c.NewConnection(*d)
err = c.NewConnection(*d)
assert.NoError(t, err, "This should not error")

c.StartTrackingTime()
Expand All @@ -127,7 +132,8 @@ Some-file-test-here
}

func TestNewConnectionCreatesTransport(t *testing.T) {
c := NewClient(NewClientConfig())
c, err := NewClient(NewClientConfig())
assert.NoError(t, err)
assert.Nil(t, c.Transport, "Transport not expected to initialized yet")

server := testServer()
Expand All @@ -141,7 +147,8 @@ func TestNewConnectionCreatesTransport(t *testing.T) {
}

func TestNewOrReusedConnectionCreatesTransport(t *testing.T) {
c := NewClient(NewClientConfig())
c, err := NewClient(NewClientConfig())
assert.NoError(t, err)
assert.Nil(t, c.Transport, "Transport not expected to initialized yet")

server := testServer()
Expand All @@ -155,7 +162,8 @@ func TestNewOrReusedConnectionCreatesTransport(t *testing.T) {
}

func TestNewOrReusedConnectionReusesTransport(t *testing.T) {
c := NewClient(NewClientConfig())
c, err := NewClient(NewClientConfig())
assert.NoError(t, err)
assert.Nil(t, c.Transport, "Transport not expected to initialized yet")

server := testServer()
Expand Down
3 changes: 2 additions & 1 deletion ftwhttp/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net"
"net/http"
Expand Down Expand Up @@ -82,7 +83,7 @@ func (c *Connection) Request(request *Request) error {
// Build request first, then connect and send, so timers are accurate
data, err := buildRequest(request)
if err != nil {
log.Fatal().Msgf("ftw/http: fatal error building request: %s", err.Error())
return fmt.Errorf("ftw/http: fatal error building request: %w", err)
}

log.Debug().Msgf("ftw/http: sending data:\n%s\n", data)
Expand Down
26 changes: 5 additions & 21 deletions ftwhttp/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,33 +183,17 @@ func buildRequest(r *Request) ([]byte, error) {
return b.Bytes(), err
}

// If the values are empty in the map, then don't encode anythin
// This keeps the compatibility with the python implementation
func emptyQueryValues(values url.Values) bool {
for _, v := range values {
val := v
if len(val) > 1 {
return false
}
}
return true
}

// encodeDataParameters url encode parameters in data
func encodeDataParameters(h Header, data []byte) ([]byte, error) {
var err error

if h.Get(ContentTypeHeader) == "application/x-www-form-urlencoded" {
if escapedData, _ := url.QueryUnescape(string(data)); escapedData == string(data) {
queryString, err := url.ParseQuery(string(data))
if (err != nil && strings.Contains(err.Error(), "invalid semicolon separator in query")) || emptyQueryValues(queryString) {
return data, nil
}
encodedData := queryString.Encode()
if encodedData != string(data) {
// we need to encode data
return []byte(encodedData), nil
if escapedData, err := url.QueryUnescape(string(data)); escapedData == string(data) {
if err != nil {
return nil, errors.New("Failed")
}
queryString := url.QueryEscape(string(data))
return []byte(queryString), nil
}
}
return data, err
Expand Down
22 changes: 22 additions & 0 deletions ftwhttp/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,25 @@ func TestRequestURLParseFail(t *testing.T) {
err := req.SetData([]byte("test=This&that=but with;;;;;; data now"))
assert.NoError(t, err)
}

func TestRequestEncodesPostData(t *testing.T) {
req := generateBaseRequestForTesting()

h := req.Headers()
h.Add(ContentTypeHeader, "application/x-www-form-urlencoded")
// Test adding semicolons to test parse
err := req.SetData([]byte(`c4= ;c3=t;c2=a;c1=c;a1=/;a2=e;a3=t;a4=c;a5=/;a6=p;a7=a;a8=s;a9=s;a10=w;a11=d;$c1$c2$c3$c4$a1$a2$a3$a4$a5$a6$a7$a8$a9$a10$a11`))
if err != nil {
t.Errorf("Failed !")
}
result, err := encodeDataParameters(h, req.Data())
if err != nil {
t.Errorf("Failed to encode %s", req.Data())
}

expected := "c4%3D+%3Bc3%3Dt%3Bc2%3Da%3Bc1%3Dc%3Ba1%3D%2F%3Ba2%3De%3Ba3%3Dt%3Ba4%3Dc%3Ba5%3D%2F%3Ba6%3Dp%3Ba7%3Da%3Ba8%3Ds%3Ba9%3Ds%3Ba10%3Dw%3Ba11%3Dd%3B%24c1%24c2%24c3%24c4%24a1%24a2%24a3%24a4%24a5%24a6%24a7%24a8%24a9%24a10%24a11"
actual := string(result)
if actual != expected {
t.Error("Unexpected URL encoded payload")
}
}
6 changes: 4 additions & 2 deletions ftwhttp/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func TestResponse(t *testing.T) {

req := generateRequestForTesting(true)

client := NewClient(NewClientConfig())
client, err := NewClient(NewClientConfig())
assert.NoError(t, err)
err = client.NewConnection(*d)
assert.NoError(t, err)

Expand All @@ -107,7 +108,8 @@ func TestResponseWithCookies(t *testing.T) {
assert.NoError(t, err)
req := generateRequestForTesting(true)

client := NewClient(NewClientConfig())
client, err := NewClient(NewClientConfig())
assert.NoError(t, err)
err = client.NewConnection(*d)

assert.NoError(t, err)
Expand Down
10 changes: 0 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main
import (
"fmt"
"os"
"time"
_ "time/tzdata"

"github.com/rs/zerolog"
Expand All @@ -29,15 +28,6 @@ func main() {
// Default level for this example is info, unless debug flag is present
zerolog.SetGlobalLevel(zerolog.InfoLevel)

// Load timezone location based on TZ
tzone, _ := time.Now().Zone()
loc, err := time.LoadLocation(tzone)
if err != nil {
log.Info().Msgf("ftw/main: cannot load timezone")
} else {
time.Local = loc // -> set the global timezone
}

cmd.Execute(
buildVersion(version, commit, date, builtBy),
)
Expand Down
Loading