-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Flc゛ <[email protected]>
- Loading branch information
Showing
12 changed files
with
137 additions
and
167 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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,47 @@ | ||
package mitake | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/go-kratos-ecosystem/components/v2/x/sms" | ||
) | ||
|
||
func TestProvider(t *testing.T) { | ||
var ( | ||
username = "test" | ||
password = "test" | ||
number = "123456789" | ||
text = "Hello, world" | ||
) | ||
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, http.MethodGet, r.Method) | ||
assert.Equal(t, username, r.URL.Query().Get("username")) | ||
assert.Equal(t, password, r.URL.Query().Get("password")) | ||
assert.Equal(t, number, r.URL.Query().Get("dstaddr")) | ||
assert.Equal(t, text, r.URL.Query().Get("smbody")) | ||
|
||
w.Write([]byte("hello")) //nolint:errcheck | ||
})) | ||
defer srv.Close() | ||
|
||
p := New(username, password, | ||
WithAPI(srv.URL), | ||
WithHTTPClient(http.DefaultClient), | ||
) | ||
|
||
err := p.Send(context.Background(), &sms.Message{ | ||
Phone: &sms.Phone{ | ||
Number: number, | ||
}, | ||
Content: &sms.Content{ | ||
Text: text, | ||
}, | ||
}) | ||
assert.NoError(t, err) | ||
} |
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,7 @@ | ||
package sms | ||
|
||
import "context" | ||
|
||
type Provider interface { | ||
Send(ctx context.Context, message *Message) error | ||
} |
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,41 @@ | ||
package sms | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
var ( | ||
ErrInvalidPhone = errors.New("sms: invalid phone") | ||
ErrInvalidMessage = errors.New("sms: invalid message") | ||
) | ||
|
||
type Phone struct { | ||
IDDCode string | ||
Number string | ||
} | ||
|
||
type Content struct { | ||
Text string | ||
Template string | ||
Variables map[string]string | ||
} | ||
|
||
type Message struct { | ||
Phone *Phone | ||
Content *Content | ||
} | ||
|
||
type Sms struct { | ||
gw Provider | ||
} | ||
|
||
func New(gw Provider) *Sms { | ||
return &Sms{ | ||
gw: gw, | ||
} | ||
} | ||
|
||
func (s *Sms) Send(ctx context.Context, message *Message) error { | ||
return s.gw.Send(ctx, message) | ||
} |
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,16 @@ | ||
package sms | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSms(t *testing.T) { | ||
sms := New(NewNullProvider()) | ||
ctx := context.Background() | ||
|
||
err := sms.Send(ctx, &Message{}) | ||
assert.NoError(t, err) | ||
} |