Skip to content

Commit

Permalink
Add: test
Browse files Browse the repository at this point in the history
  • Loading branch information
chinanf-boy committed Nov 8, 2018
1 parent 8f487be commit 563d0c7
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 62 deletions.
25 changes: 12 additions & 13 deletions pkg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var config = Config{

// Socks5Client > get httpClient with socks5
func Socks5Client() *http.Client {
u, e := getURL()
u, e := getURL(config)
if e != nil {
log.Fatalln("Proxy Env Set Error", e)
}
Expand All @@ -69,26 +69,26 @@ func GetProxis() Config {
return config
}

func getURL() (*url.URL, error) {
func getURL(c Config) (*url.URL, error) {
var u *url.URL
var e error
if len(config.ALLProxy) > 0 {
u, e = url.Parse(config.ALLProxy)
} else if len(config.HTTPSProxy) > 0 {
u, e = url.Parse(config.HTTPSProxy)
if len(c.ALLProxy) > 0 {
u, e = url.Parse(c.ALLProxy)
} else if len(c.HTTPSProxy) > 0 {
u, e = url.Parse(c.HTTPSProxy)

} else if len(config.HTTPProxy) > 0 {
u, e = url.Parse(config.HTTPProxy)
} else if len(c.HTTPProxy) > 0 {
u, e = url.Parse(c.HTTPProxy)
}

return u, e
}

func whichProxy() string {
func (c Config) whichProxy() string {
var result string
if isSocks(config.ALLProxy) ||
isSocks(config.HTTPProxy) ||
isSocks(config.HTTPSProxy) {
if isSocks(c.ALLProxy) ||
isSocks(c.HTTPProxy) ||
isSocks(c.HTTPSProxy) {
result = SOCKS
} else {
result = HTTP
Expand All @@ -98,7 +98,6 @@ func whichProxy() string {

func isSocks(s string) bool {
b := isRegexp(s, `^socks`)

return b
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package howdoi

import (
"os"
"testing"
)

func TestCientUtil(t *testing.T) {
var config = Config{
HTTPProxy: getEnvAny("HTTP_PROXY", "http_proxy"),
HTTPSProxy: getEnvAny("HTTPS_PROXY", "https_proxy"),
NoProxy: getEnvAny("NO_PROXY", "no_proxy"),
CGI: os.Getenv("REQUEST_METHOD") != "",
ALLProxy: getEnvAny("ALL_PROXY", "all_proxy"),
}
a := "NOTHINGNOTHINGNOTHING"
res := getEnvAny(a, a)

if res != "" {
t.Fail()
}
_, err := getURL(config)

if err != nil {
t.Fail()
}
}
121 changes: 114 additions & 7 deletions pkg/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,60 @@ package howdoi
import (
"fmt"
"os"
"strconv"
"testing"
)

func TestExample(t *testing.T) {
func newArg(s int) []string {
// []string{"-q", "format date bash", "-c", "-C"}
nArr := make([]string, 0)
nArr = append(nArr, os.Args[0])
args := []string{"format date bash"}

for _, v := range args {
nArr = append(nArr, "-q")
nArr = append(nArr, v)
}
if s > 0 {
nArr = append(nArr, "-c")
nArr = append(nArr, "-T")
nArr = append(nArr, "github")
nArr = append(nArr, "-C")
nArr = append(nArr, "-n")
nArr = append(nArr, "3")
}
if s > 1 {
nArr = append(nArr, "-R")
nArr = append(nArr, "-D")
}
fmt.Println("new args", nArr)

return nArr
}

func TestExampleVersion(t *testing.T) {

// use Lib for howdoi, ArgsPar get the Cli struct
nArr := make([]string, 0)
exampleArgs := append(nArr, os.Args[0])
exampleArgs = append(exampleArgs, "-v")

exampleArgs := append(os.Args, "-q")
exampleArgs = append(exampleArgs, "format date bash")
res, err := ArgsPar(exampleArgs)

if res.Version {
return
}

if err != nil {
fmt.Println("args parse fail", err)
t.Fail()
}
}

func TestExample(t *testing.T) {

// use Lib for howdoi, ArgsPar get the Cli struct
exampleArgs := newArg(1)

res, err := ArgsPar(exampleArgs)

Expand All @@ -29,12 +74,74 @@ func TestExample(t *testing.T) {

if err != nil {
fmt.Println("howdoi fail", err)
t.Fail()
}
}

func TestExampleDebug(t *testing.T) {

// use Lib for howdoi, ArgsPar get the Cli struct
exampleArgs := newArg(2)

res, err := ArgsPar(exampleArgs)

if res.Version {
return
}

if err != nil {
fmt.Println("args parse fail", err)
t.Fail()
}

// pass Cli
_, err = Howdoi(res)

if err != nil {
fmt.Println("howdoi fail", err)
t.Fail()
}
}

func TestChanExample(t *testing.T) {

// use Lib for howdoi, ArgsPar get the Cli struct
exampleArgs := newArg(1)

res, err := ArgsPar(exampleArgs)

if res.Version {
return
}

cliChan := make(chan string)
done := make(chan int)

if err != nil {
t.Fatal(err)
}
go func() {
n := 0
for i := 0; i < 1; i++ {
s := <-cliChan
n, _ = strconv.Atoi(s)
// fmt.Printf("form ChanHowdoi %s\n", s)
}

for i := 0; i < n; i++ {
<-cliChan

}
close(cliChan)
done <- 0

// for _, v := range result {
// fmt.Println()
// fmt.Println(v)
// }
}()
// pass Cli
err = ChanHowdoi(res, cliChan)

if err != nil {
t.Fatal(err)
}
<-done
close(done)
}
41 changes: 40 additions & 1 deletion pkg/getAndParse.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (clis Cli) getResult(u string) (doc *goquery.Document, reqErr error) {
}
req.Header.Set("User-Agent", getRandomUA())

proxyIs := whichProxy()
proxyIs := config.whichProxy()
if proxyIs == SOCKS {
httpClient := Socks5Client()
resp, err = httpClient.Do(req)
Expand Down Expand Up @@ -210,3 +210,42 @@ func (clis Cli) getResult(u string) (doc *goquery.Document, reqErr error) {

return
}

func extractLinks(doc *goquery.Document, engine string) []string {
gLog := debug.Debug("extractLinks")

var links []string
if engine == "bing" {
doc.Find("a").Each(func(i int, s *goquery.Selection) {
attr, exists := s.Attr("href")
if exists == true && isQuestion(attr) {
links = append(links, attr)
}
})
} else {
one := doc.Find("a")
if one.Size() > 0 {
one.Each(func(i int, s *goquery.Selection) {
attr, exists := s.Attr("href")

if exists == true && isQuestion(attr) {
links = append(links, attr)
}
})
}
}

gLog("%s, extract link %d", engine, len(links))

// Cache what you got
// if len(links) == 0 {
// s, _ := doc.Html()
// gLog("page Hava text number %d", len(s))
// f, _ := os.Create("./index.html")
// n, _ := f.WriteString(s)

// redLog(string(n))

// }
return links
}
40 changes: 0 additions & 40 deletions pkg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"os"
"regexp"

"github.com/PuerkitoBio/goquery"
"github.com/logrusorgru/aurora"
debug "github.com/visionmedia/go-debug"
)

func getEnv(key, fallback string) string {
Expand Down Expand Up @@ -54,44 +52,6 @@ func isQuestion(s string) bool {
func getSearchURL(s string) string {
return getMapDef(searchUrls, s, searchUrls["bing"])
}
func extractLinks(doc *goquery.Document, engine string) []string {
gLog := debug.Debug("extractLinks")

var links []string
if engine == "bing" {
doc.Find("a").Each(func(i int, s *goquery.Selection) {
attr, exists := s.Attr("href")
if exists == true && isQuestion(attr) {
links = append(links, attr)
}
})
} else {
one := doc.Find("a")
if one.Size() > 0 {
one.Each(func(i int, s *goquery.Selection) {
attr, exists := s.Attr("href")

if exists == true && isQuestion(attr) {
links = append(links, attr)
}
})
}
}

gLog("%s, extract link %d", engine, len(links))

// Cache what you got
// if len(links) == 0 {
// s, _ := doc.Html()
// gLog("page Hava text number %d", len(s))
// f, _ := os.Create("./index.html")
// n, _ := f.WriteString(s)

// redLog(string(n))

// }
return links
}

// UqineSlice remove same string with slice
func UqineSlice(elements []string) []string {
Expand Down
21 changes: 21 additions & 0 deletions pkg/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,24 @@ func TestGetEnv(t *testing.T) {
t.Fail()
}
}

func TestUqineSlice(t *testing.T) {
old := []string{"h", "o", "o", "l"}
res := UqineSlice(old)
if len(res) != 3 {
t.Fail()
}
}

func TestColorString(t *testing.T) {

r := red("rea string")

g := gree("green string")
c := cyan("cyan string")
f := format("format %s", "str")

if len(r) == 0 || len(g) == 0 || len(c) == 0 || len(f) == 0 {
t.Fail()
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ go get -v -u github.com/chinanf-boy/howdoi
- [x] ReCache Result ?
- [x] **go** func with ALL engines
- [x] add **ChanHowdoi**: got one result, show it, rather than all results
- [ ] test file
- [x] test file
- [ ] [Issue me anything](https://github.com/chinanf-boy/howdoi/issues/new)

> **Notes:** , cli-name Over the Python version cli
Expand Down

0 comments on commit 563d0c7

Please sign in to comment.