0

I am running Android UI tests. I use a separate Firestore DB for testing. I want to delete all documents after a test finishes. This curl command works.

curl -v -X DELETE "http://127.0.0.1:8080/emulator/v1/projects/test/databases/(default)/documents"

I want to use this in a method in my test class.

class MainActivityTest {
    @Rule
    // rule goes here

    @BeforeClass
    public static void setUp() {
        String androidLocalHost = "10.0.2.2";
        int portNumber = 8080;
        FirebaseFirestore.getInstance().useEmulator(androidLocalHost, portNumber);
        Log.d("DBT", "Connected to local Firestore");
    }
    
    @After
    public void tearDown() {
        String projectId = "test";
        URL url = null;
        try {
            url = new URL("http://10.0.2.2:8080/emulator/v1/projects/" + projectId + "/databases/(default)/documents");
        } catch (MalformedURLException exception) {
            Log.e("URLError", Objects.requireNonNull(exception.getMessage()));
        }
        HttpURLConnection urlConnection = null;
        Log.d("TeardownURL", "Connecting to: " + url.toString());
        try {
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("DELETE");
            int response = urlConnection.getResponseCode();
            String msg = urlConnection.getResponseMessage();
            Log.i("ResponseCode", "[Response Code: " + response + "] msg: " + msg);
        } catch (IOException exception) {
            Log.e("IOError", Objects.requireNonNull(exception.getMessage()));
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
    }
}

I added network_security_config.xml to allow cleartext and include the host 10.0.2.2 and updated the manifest file accordingly.

However, I get 404 Not found (in ResponseCode log).

My curl command works fine. The only difference is curl uses localhost/127.0.0.1. Since this is in Android studio, I use 10.0.2.2.

0

1 Answer 1

0

After a couple of rounds of prompting with Gemini, I found the solution. I needed to include this line after the urlConnection.requestMethod() call.

urlConnection.setRequestProperty("Content-Type", "application/json");

Now, I am getting the 200 OK response code/msg.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.