-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
including comments, mergin testing email service with new smtp servic…
…e and improve testing
- Loading branch information
1 parent
2718ec7
commit 715917c
Showing
8 changed files
with
150 additions
and
118 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 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 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,73 @@ | ||
package smtp | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
searchInboxTestEndpoint = "http://%s:%d/api/v2/search?kind=to&query=%s" | ||
clearInboxTestEndpoint = "http://%s:%d/api/v1/messages" | ||
) | ||
|
||
// FindEmail searches for an email in the test API service. It sends a GET | ||
// request to the search endpoint with the recipient's email address as a query | ||
// parameter. If the email is found, it returns the email body and clears the | ||
// inbox. If the email is not found, it returns an EOF error. If the request | ||
// fails, it returns an error with the status code. This method is used for | ||
// testing the email service. | ||
func (sm *SMTPEmail) FindEmail(ctx context.Context, to string) (string, error) { | ||
searchEndpoint := fmt.Sprintf(searchInboxTestEndpoint, sm.config.SMTPServer, sm.config.TestAPIPort, to) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, searchEndpoint, nil) | ||
if err != nil { | ||
return "", fmt.Errorf("could not create request: %v", err) | ||
} | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return "", fmt.Errorf("could not send request: %v", err) | ||
} | ||
defer func() { | ||
_ = resp.Body.Close() | ||
}() | ||
if resp.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
|
||
type mailResponse struct { | ||
Items []struct { | ||
Content struct { | ||
Body string `json:"Body"` | ||
} `json:"Content"` | ||
} `json:"items"` | ||
} | ||
mailResults := mailResponse{} | ||
if err := json.NewDecoder(resp.Body).Decode(&mailResults); err != nil { | ||
return "", fmt.Errorf("could not decode response: %v", err) | ||
} | ||
if len(mailResults.Items) == 0 { | ||
return "", io.EOF | ||
} | ||
return mailResults.Items[0].Content.Body, sm.clear() | ||
} | ||
|
||
func (sm *SMTPEmail) clear() error { | ||
clearEndpoint := fmt.Sprintf(clearInboxTestEndpoint, sm.config.SMTPServer, sm.config.TestAPIPort) | ||
req, err := http.NewRequest(http.MethodDelete, clearEndpoint, nil) | ||
if err != nil { | ||
return fmt.Errorf("could not create request: %v", err) | ||
} | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("could not send request: %v", err) | ||
} | ||
defer func() { | ||
_ = resp.Body.Close() | ||
}() | ||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.