Skip to content

Commit be92824

Browse files
committed
[Core] Rename Mime-Type to Media-Type
Embeddings should be provided with some information to let formatters and reporters know how to interpret the bytes they receive. This used to be called `Mime-Type`. However since then as a standard the `Mime-Type` has been upgraded and renamed to `Media-Type`. By renaming `Mime-Type` to `Media-Type` we make it clear that consumer and producers of embeddings should use the semantics outlined in: https://tools.ietf.org/html/rfc7231#section-3.1.1.1
1 parent 5ae5cb5 commit be92824

File tree

10 files changed

+91
-68
lines changed

10 files changed

+91
-68
lines changed

core/src/main/java/io/cucumber/core/backend/TestCaseState.java

+23-26
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@
55
import java.net.URI;
66
import java.util.Collection;
77

8-
/**
9-
* Before or After Hooks that declare a parameter of this type will receive an instance of this class.
10-
* It allows writing text and embedding media into reports, as well as inspecting results (in an After block).
11-
* <p>
12-
* Note: This class is not intended to be used to create reports. To create custom reports use
13-
* the {@code io.cucumber.plugin.Plugin} class. The plugin system provides a much richer access to Cucumbers then
14-
* hooks after could provide. For an example see {@code io.cucumber.core.plugin.PrettyFormatter}.
15-
*/
8+
169
@API(status = API.Status.STABLE)
1710
public interface TestCaseState {
1811
/**
@@ -31,32 +24,36 @@ public interface TestCaseState {
3124
boolean isFailed();
3225

3326
/**
34-
* Embeds data into the report(s). Some reporters (such as the progress one) don't embed data, but others do (html and json).
35-
* Example:
36-
*
27+
* @param data what to embed, for example an image.
28+
* @param mediaType what is the data? Using the
29+
* @see #embed(byte[], String, String)
30+
* @see #embed(byte[], String, String)
31+
* @deprecated use {@link TestCaseState#embed(byte[], String, String)} instead.
32+
*/
33+
@Deprecated
34+
void embed(byte[] data, String mediaType);
35+
36+
/**
37+
* Embeds data into the report(s).
3738
* <pre>
3839
* {@code
3940
* // Embed a screenshot. See your UI automation tool's docs for
4041
* // details about how to take a screenshot.
41-
* scenario.embed(pngBytes, "image/png");
42+
* scenario.embed(pngBytes, "image/png", "Bartholomew and the Bytes of the Oobleck");
4243
* }
4344
* </pre>
45+
* <p>
46+
* To ensure reporting tools can understand what the data is a
47+
* {@code mediaType} must be provided. For example: {@code text/plain},
48+
* {@code image/png}, {@code text/html;charset=utf-8}.
49+
* <p>
50+
* Media types are defined in <a href= https://tools.ietf.org/html/rfc7231#section-3.1.1.1>RFC 7231 Section 3.1.1.1</a>.
4451
*
45-
* @param data what to embed, for example an image.
46-
* @param mimeType what is the data?
47-
* @deprecated use {@link TestCaseState#embed(byte[], String, String)} instead.
48-
*/
49-
@Deprecated
50-
void embed(byte[] data, String mimeType);
51-
52-
/**
53-
* Like {@link TestCaseState#embed(byte[], String)}, but with name for the embedding.
54-
*
55-
* @param data what to embed, for example an image.
56-
* @param mimeType what is the data?
57-
* @param name embedding name
52+
* @param data what to embed, for example an image.
53+
* @param mediaType what is the data?
54+
* @param name embedding name
5855
*/
59-
void embed(byte[] data, String mimeType, String name);
56+
void embed(byte[] data, String mediaType, String name);
6057

6158
/**
6259
* Outputs some text into the report.

core/src/main/java/io/cucumber/core/plugin/HTMLFormatter.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,17 @@ private String getFunctionName(HookTestStep hookTestStep) {
165165
}
166166

167167
private void handleEmbed(EmbedEvent event) {
168-
String mimeType = event.getMimeType();
169-
if (mimeType.startsWith("text/")) {
168+
String mediaType = event.getMediaType();
169+
if (mediaType.startsWith("text/")) {
170170
// just pass straight to the plugin to output in the html
171-
jsFunctionCall("embedding", mimeType, new String(event.getData()), event.getName());
171+
jsFunctionCall("embedding", mediaType, new String(event.getData()), event.getName());
172172
} else {
173173
// Creating a file instead of using data urls to not clutter the js file
174-
String extension = MIME_TYPES_EXTENSIONS.get(mimeType);
174+
String extension = MIME_TYPES_EXTENSIONS.get(mediaType);
175175
if (extension != null) {
176176
StringBuilder fileName = new StringBuilder("embedded").append(embeddedIndex++).append(".").append(extension);
177177
writeBytesToURL(event.getData(), toUrl(fileName.toString()));
178-
jsFunctionCall("embedding", mimeType, fileName, event.getName());
178+
jsFunctionCall("embedding", mediaType, fileName, event.getName());
179179
}
180180
}
181181
}

core/src/main/java/io/cucumber/core/plugin/JSONFormatter.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private void handleWrite(WriteEvent event) {
124124
}
125125

126126
private void handleEmbed(EmbedEvent event) {
127-
addEmbeddingToHookMap(event.getData(), event.getMimeType(), event.getName());
127+
addEmbeddingToHookMap(event.getData(), event.getMediaType(), event.getName());
128128
}
129129

130130
private void handleTestStepFinished(TestStepFinished event) {
@@ -291,17 +291,17 @@ private void addOutputToHookMap(String text) {
291291
((List<String>) currentStepOrHookMap.get("output")).add(text);
292292
}
293293

294-
private void addEmbeddingToHookMap(byte[] data, String mimeType, String name) {
294+
private void addEmbeddingToHookMap(byte[] data, String mediaType, String name) {
295295
if (!currentStepOrHookMap.containsKey("embeddings")) {
296296
currentStepOrHookMap.put("embeddings", new ArrayList<Map<String, Object>>());
297297
}
298-
Map<String, Object> embedMap = createEmbeddingMap(data, mimeType, name);
298+
Map<String, Object> embedMap = createEmbeddingMap(data, mediaType, name);
299299
((List<Map<String, Object>>) currentStepOrHookMap.get("embeddings")).add(embedMap);
300300
}
301301

302-
private Map<String, Object> createEmbeddingMap(byte[] data, String mimeType, String name) {
302+
private Map<String, Object> createEmbeddingMap(byte[] data, String mediaType, String name) {
303303
Map<String, Object> embedMap = new HashMap<>();
304-
embedMap.put("mime_type", mimeType);
304+
embedMap.put("mime_type", mediaType); // Should be media-type but not worth migrating for
305305
embedMap.put("data", Base64.getEncoder().encodeToString(data));
306306
if (name != null) {
307307
embedMap.put("name", name);

core/src/main/java/io/cucumber/core/runner/TestCaseState.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public boolean isFailed() {
5353

5454
@Deprecated
5555
@Override
56-
public void embed(byte[] data, String mimeType) {
57-
bus.send(new EmbedEvent(bus.getInstant(), testCase, data, mimeType));
56+
public void embed(byte[] data, String mediaType) {
57+
bus.send(new EmbedEvent(bus.getInstant(), testCase, data, mediaType));
5858
}
5959

6060
@Override
61-
public void embed(byte[] data, String mimeType, String name) {
62-
bus.send(new EmbedEvent(bus.getInstant(), testCase, data, mimeType, name));
61+
public void embed(byte[] data, String mediaType, String name) {
62+
bus.send(new EmbedEvent(bus.getInstant(), testCase, data, mediaType, name));
6363
}
6464

6565
@Override

core/src/main/resources/io/cucumber/core/plugin/html/formatter.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ CucumberHTML.DOMFormatter = function(rootNode) {
9393
}
9494
};
9595

96-
this.embedding = function(mimeType, data, name) {
96+
this.embedding = function(mediaType, data, name) {
9797
var nameHtml;
9898
if (!name) {
9999
nameHtml = "";
@@ -103,15 +103,15 @@ CucumberHTML.DOMFormatter = function(rootNode) {
103103
if (currentStepIndex == 1) {
104104
this.dummyStep();
105105
}
106-
if (mimeType.match(/^image\//))
106+
if (mediaType.match(/^image\//))
107107
{
108108
currentStep.append(nameHtml + '<img src="' + data + '">');
109109
}
110-
else if (mimeType.match(/^video\//))
110+
else if (mediaType.match(/^video\//))
111111
{
112-
currentStep.append(nameHtml + '<video src="' + data + '" type="' + mimeType + '" autobuffer controls>Your browser doesn\'t support video.</video>');
112+
currentStep.append(nameHtml + '<video src="' + data + '" type="' + mediaType + '" autobuffer controls>Your browser doesn\'t support video.</video>');
113113
}
114-
else if (mimeType.match(/^text\//))
114+
else if (mediaType.match(/^text\//))
115115
{
116116
this.write(nameHtml + data);
117117
}

core/src/test/java/io/cucumber/core/runner/TestCaseStateResultTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,17 @@ void pending_followed_by_failed_yields_failed_error() {
155155

156156
private static final class EmbedEventMatcher implements ArgumentMatcher<EmbedEvent> {
157157
private byte[] data;
158-
private String mimeType;
158+
private String mediaType;
159159

160-
EmbedEventMatcher(byte[] data, String mimeType) {
160+
EmbedEventMatcher(byte[] data, String mediaType) {
161161
this.data = data;
162-
this.mimeType = mimeType;
162+
this.mediaType = mediaType;
163163
}
164164

165165
@Override
166166
public boolean matches(EmbedEvent argument) {
167167
return (argument != null &&
168-
Arrays.equals(argument.getData(), data) && argument.getMimeType().equals(mimeType));
168+
Arrays.equals(argument.getData(), data) && argument.getMediaType().equals(mediaType));
169169
}
170170
}
171171

core/src/test/java/io/cucumber/core/runner/TestHelper.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,18 @@ public static Answer<Object> createWriteHookAction(final String output) {
116116
};
117117
}
118118

119-
public static Answer<Object> createEmbedHookAction(final byte[] data, final String mimeType) {
120-
return createEmbedHookAction(data, mimeType, null);
119+
public static Answer<Object> createEmbedHookAction(final byte[] data, final String mediaType) {
120+
return createEmbedHookAction(data, mediaType, null);
121121
}
122122

123123
@SuppressWarnings("deprecation")
124-
public static Answer<Object> createEmbedHookAction(final byte[] data, final String mimeType, final String name) {
124+
public static Answer<Object> createEmbedHookAction(final byte[] data, final String mediaType, final String name) {
125125
return invocation -> {
126126
TestCaseState state = (TestCaseState) invocation.getArguments()[0];
127127
if (name != null) {
128-
state.embed(data, mimeType, name);
128+
state.embed(data, mediaType, name);
129129
} else {
130-
state.embed(data, mimeType);
130+
state.embed(data, mediaType);
131131
}
132132
return null;
133133
};

java/src/main/java/io/cucumber/java/Scenario.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
import java.net.URI;
77
import java.util.Collection;
88

9+
/**
10+
* Before or After Hooks that declare a parameter of this type will receive an instance of this class.
11+
* It allows writing text and embedding media into reports, as well as inspecting results (in an After block).
12+
* <p>
13+
* Note: This class is not intended to be used to create reports. To create custom reports use
14+
* the {@code io.cucumber.plugin.Plugin} class. The plugin system provides a much richer access to Cucumbers then
15+
* hooks after could provide. For an example see {@code io.cucumber.core.plugin.PrettyFormatter}.
16+
*/
917
@API(status = API.Status.STABLE)
1018
public final class Scenario {
1119

@@ -28,12 +36,12 @@ public boolean isFailed() {
2836
}
2937

3038
@Deprecated
31-
public void embed(byte[] data, String mimeType) {
32-
delegate.embed(data, mimeType);
39+
public void embed(byte[] data, String mediaType) {
40+
delegate.embed(data, mediaType);
3341
}
3442

35-
public void embed(byte[] data, String mimeType, String name) {
36-
delegate.embed(data, mimeType, name);
43+
public void embed(byte[] data, String mediaType, String name) {
44+
delegate.embed(data, mediaType, name);
3745
}
3846

3947
public void write(String text) {

java8/src/main/java/io/cucumber/java8/Scenario.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
import java.net.URI;
77
import java.util.Collection;
88

9+
/**
10+
* Before or After Hooks that declare a parameter of this type will receive an instance of this class.
11+
* It allows writing text and embedding media into reports, as well as inspecting results (in an After block).
12+
* <p>
13+
* Note: This class is not intended to be used to create reports. To create custom reports use
14+
* the {@code io.cucumber.plugin.Plugin} class. The plugin system provides a much richer access to Cucumbers then
15+
* hooks after could provide. For an example see {@code io.cucumber.core.plugin.PrettyFormatter}.
16+
*/
917
@API(status = API.Status.STABLE)
1018
public final class Scenario {
1119

@@ -28,12 +36,12 @@ public boolean isFailed() {
2836
}
2937

3038
@Deprecated
31-
public void embed(byte[] data, String mimeType) {
32-
delegate.embed(data, mimeType);
39+
public void embed(byte[] data, String mediaType) {
40+
delegate.embed(data, mediaType);
3341
}
3442

35-
public void embed(byte[] data, String mimeType, String name) {
36-
delegate.embed(data, mimeType, name);
43+
public void embed(byte[] data, String mediaType, String name) {
44+
delegate.embed(data, mediaType, name);
3745
}
3846

3947
public void write(String text) {

plugin/src/main/java/io/cucumber/plugin/event/EmbedEvent.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,39 @@
88
@API(status = API.Status.STABLE)
99
public final class EmbedEvent extends TestCaseEvent {
1010
private final byte[] data;
11-
private final String mimeType;
11+
private final String mediaType;
1212
public final String name;
1313

14-
public EmbedEvent(Instant timeInstant, TestCase testCase, byte[] data, String mimeType) {
14+
public EmbedEvent(Instant timeInstant, TestCase testCase, byte[] data, String mediaType) {
1515
super(timeInstant, testCase);
1616
this.data = Objects.requireNonNull(data);
17-
this.mimeType = Objects.requireNonNull(mimeType);
17+
this.mediaType = Objects.requireNonNull(mediaType);
1818
this.name = null;
1919
}
2020

21-
public EmbedEvent(Instant timeInstant, TestCase testCase, byte[] data, String mimeType, String name) {
21+
public EmbedEvent(Instant timeInstant, TestCase testCase, byte[] data, String mediaType, String name) {
2222
super(timeInstant, testCase);
2323
this.data = data;
24-
this.mimeType = mimeType;
24+
this.mediaType = mediaType;
2525
this.name = name;
2626
}
2727

2828
public byte[] getData() {
2929
return data;
3030
}
3131

32+
public String getMediaType() {
33+
return mediaType;
34+
}
35+
36+
/**
37+
* @deprecated use {@link #getMediaType()}
38+
*
39+
* @return media type of the embedding.
40+
*/
41+
@Deprecated
3242
public String getMimeType() {
33-
return mimeType;
43+
return mediaType;
3444
}
3545

3646
public String getName() {

0 commit comments

Comments
 (0)