Skip to content

Commit f0d346b

Browse files
authored
Added method to Internet to create RFC2822 compliant email message ids (#1)
1 parent a8b8ff0 commit f0d346b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/main/java/com/github/javafaker/Internet.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ private String emailAddress(String localPart, String domain) {
3838
return join(stripAccents(localPart), "@", domain);
3939
}
4040

41+
/**
42+
* Generates a message-id (msg-id) valid per https://tools.ietf.org/html/rfc2822
43+
*
44+
* @param domain The domain name to use in the id-right part of the message-id
45+
* @return A valid message id with the supplied domain
46+
*/
47+
public String rfcMessageId(String domain) {
48+
return join("<", UUID.randomUUID(), "@", domain, ">");
49+
}
50+
51+
/**
52+
* Generates a message-id (msg-id) valid per https://tools.ietf.org/html/rfc2822
53+
*
54+
* @return A valid message id with a faker generated domain
55+
*/
56+
public String rfcMessageId() {
57+
return rfcMessageId(domainName());
58+
}
59+
4160
public String domainName() {
4261
return domainWord() + "." + domainSuffix();
4362
}

src/test/java/com/github/javafaker/InternetTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ public void testImage() {
8888
assertThat(imageUrl, matchesRegularExpression("^http:\\/\\/lorempixel\\.com(/g)?/\\d{1,4}/\\d{1,4}/\\w+/$"));
8989
}
9090

91+
@Test
92+
public void testRfc2822MessageIdIsValid() {
93+
String rfcMessageId = faker.internet().rfcMessageId();
94+
assertThat(rfcMessageId, matchesRegularExpression("<.*@.*\\..*>"));
95+
}
96+
97+
@Test
98+
public void testRfc2822MessageIdIsValidSuppliedDomain() {
99+
String rfcMessageId = faker.internet().rfcMessageId("mydomain.org");
100+
assertThat(rfcMessageId, matchesRegularExpression("<.*@.*\\..*>"));
101+
}
102+
91103
@Test
92104
public void testDomainName() {
93105
assertThat(faker.internet().domainName(), matchesRegularExpression("[a-z]+\\.\\w{2,4}"));

0 commit comments

Comments
 (0)