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

Changes hping so it accepts host:port and fixes the test for hping so… #34

Merged
merged 1 commit into from
Oct 11, 2016
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
13 changes: 10 additions & 3 deletions http/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,22 @@ func NewPing(args string, cfg cli.Config) (*Ping, error) {
if err != nil {
return &Ping{}, fmt.Errorf("cannot parse url")
}

host, _, err := net.SplitHostPort(u.Host)
if err != nil {
host = u.Host
}

sTime := time.Now()
ipAddr, err := net.ResolveIPAddr("ip", u.Host)

ipAddr, err := net.ResolveIPAddr("ip", host)
if err != nil {
return &Ping{}, fmt.Errorf("cannot resolve %s: Unknown host", u.Host)
return &Ping{}, fmt.Errorf("cannot resolve %s: Unknown host", host)
}

p := &Ping{
url: URL,
host: u.Host,
host: host,
rAddr: ipAddr,
buf: cli.SetFlag(flag, "d", "mylg").(string),
count: cli.SetFlag(flag, "c", cfg.Hping.Count).(int),
Expand Down
22 changes: 15 additions & 7 deletions http/ping/ping_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package ping_test

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/mehrdadrad/mylg/cli"
"github.com/mehrdadrad/mylg/http/ping"
"gopkg.in/h2non/gock.v0"
)

func TestNewPing(t *testing.T) {
Expand All @@ -21,16 +23,22 @@ func TestNewPing(t *testing.T) {
}

func TestPing(t *testing.T) {
var url = "google.com"
gock.New(url).
Reply(301)
testHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintln(w, "test")
}

ts := httptest.NewServer(http.HandlerFunc(testHandler))
defer ts.Close()

cfg, _ := cli.ReadDefaultConfig()
p, _ := ping.NewPing(url, cfg)
p, _ := ping.NewPing(ts.URL, cfg)
r, _ := p.Ping()
if r.StatusCode != 301 {
t.Error("PingGet expected to get 301 but didn't, I got:", r.StatusCode)

if r.StatusCode != 200 {
t.Error("PingGet expected to get 200 but didn't, I got:", r.StatusCode)
}

if r.TotalTime == 0 {
t.Error("PingGet expected to set totaltime but it didn't")
}
Expand Down